Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between @ TableName and @ Table

2025-03-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the relevant knowledge of "what's the difference between @ TableName and @ Table". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "what's the difference between @ TableName and @ Table" can help you solve the problem.

The difference between @ TableName annotations and @ Table

When you recently developed a project, you migrated code from one project to another, using Hibernate for the original project and mybatis-plus for the existing project. After the code was migrated, the schema attribute was used to specify the database instance name.

But the @ Table attribute schema has failed in the mybatis-plus framework and the corresponding instance name has not been found.

Preface

@ TableName is an annotation in mybatis-plus that implements mapping between entity types and tables in the database.

Be careful not to think of @ TableName and @ Table annotations as one. Although the functions are the same, @ TableName is the annotation in mybatis-plus and @ Table is the annotation in Hibernate.

@ TableName parameter description

@ TableName source code

Public @ interface TableName {java.lang.String value () default ""; java.lang.String schema () default ""; boolean keepGlobalPrefix () default false; java.lang.String resultMap () default ""; boolean autoResultMap () default false;}

@ TableName use

@ TableName ("sys_user") / / this annotation is mainly a mapping between the real entity type and the table implementation in the database. Public class SysUser implements Serializable {@ TableId (type= IdType.AUTO) private Long id; private String username; / / username private String loginName; / / login password, password needs to be encrypted private String password;} @ Table

You need to use the @ Table annotation when the name of the entity class is different from the name of the database table it maps to. This annotation is used side by side with the @ Entity annotation and is placed before the entity class declaration statement. It can be written on a separate statement line or in the same line as the declaration statement.

The common option for the @ Table annotation is name, which indicates the table name of the database

The @ Table annotation also has two options, catalog and schema, to set the database directory or schema to which the table belongs, usually the database name. The uniqueConstraints option is used to set constraints, which are usually not required.

Name attribute

The name property is used to specify the database table name

If not specified, the entity class name is used as the table name

Catalog attribute

The catalog property is used to specify the database instance name

When the catalog property is not specified, the newly created table will appear in the database instance specified by url

When the catalog property sets the name, the newly created table will appear in the instance if there is an instance of the database that matches the specified name.

Schema attribute

The function is consistent with the catalog attribute, and can be tested by yourself.

UniqueConstraints attribute

The uniqueConstraints property is used to set constraints

@ Table (name= "CUSTOMERS", uniqueConstraints= {@ UniqueConstraint (columnNames= {"name"})}) @ Entitypublic class Customer {private Integer id; private String name; private String email; private int age;.}

1. When the entity class name is inconsistent with the database table name, the name attribute can be mapped, even if the table name is consistent, and it is also recommended to use it to improve the readability of the program.

The 2.catalog and sechema properties generally do not need to be specified

The 3.uniqueConstraints attribute generally does not need to be specified, but can be specified if necessary

Table (name= "xx") and Entity (name= "xx") questions @ Entity@Table (name= "T_qibie")

If you write it this way, use the name from QibieVo of the entity in hql.

If that's the case.

@ Entity (name= "T_qibie")

If you write it this way, use the name of the table from T_qibie in hql.

And sometimes if you can only use @ Entity (name= "T_qibie"), it may be: the javax.persistence.jar in JAVAEE6.0 conflicts with the hibernate-jpa-2.1-api-1.0.0.Final.jar in hibernate, and the measure I take is to delete the JAVAEE6.0 library, run normally, and then add it to it.

That's all for "what's the difference between @ TableName and @ Table". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report