In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Most people do not understand the knowledge points of this article "how to use @ Entity and @ Table annotations", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "@ Entity and @ Table annotations" article.
The use of @ Entity and @ Table annotations @ Entity annotations
The @ Entity annotation and the @ Table annotation are both annotations defined in Java Persistence API.
@ Entity indicates that the class is an entity class and uses the default orm rule, that is, the class name is indicated in the database table, and the class field name is the field name in the table. The @ Entity annotation indicates that this is an entity Bean.
Import lombok.Data; import javax.persistence.*; @ Data@Entity (name = "t_book") public class Book {@ Id @ GeneratedValue (strategy = GenerationType.IDENTITY) private Integer id; @ Column (name = "book_name") private String name; @ Column (name = "book_author") private String author; private Float price; @ Transient private String description;}
The @ Entity annotation indicates that the class is for the entity class. When envy starts, a table is automatically generated based on the class. The name of the table is the value of name in the @ Entity annotation. If name is not configured, the default is the class name.
All entity classes must have a primary key. The @ Id annotation indicates that the attribute is a primary key, the @ GeneratedValue annotation indicates automatic annotation generation, and strategy indicates the primary key generation strategy.
By default, the name of the field in the generated table is the name of the attribute in the entity class. The generated field attribute can be customized through the @ Column annotation. Name indicates the name of the field in the data table corresponding to the attribute, and nullable indicates that the field is not empty.
The @ Transient annotation indicates that this attribute is ignored when generating a table in the database, that is, the corresponding field is not generated
@ Table comment
The @ Table annotation only completes the mapping between tables and entities by default, and declares that objects are mapped to database tables, through which you can specify tables (table) for entities.
Data@Table (name= "book") public class Book {@ Id private Integer id; @ Column (name= "book_name") private String name;.}
Common attribute: name specification table
@ Table (name = "book")
The @ Table annotation is an optional annotation, and the @ Table annotation specifies the database table to be mapped by Entity, where @ Table.name () is used to specify the table name of the mapping table.
If @ Entity (name= "student") and @ Table (name= "book") are used at the same time, the corresponding table name is book, which indicates the priority: @ Table > @ Entity
The difference between @ Entity and @ Table
Java Persistence API defines a definition that maps regular ordinary Java objects (sometimes called POJO) to a database. These ordinary Java objects are called Entity Bean. Entity Bean is no different from other Java classes except that it is mapped to a database with Java Persistence metadata.
In fact, creating an Entity Bean object is equivalent to creating a new record, deleting an Entity Bean will also delete the corresponding record from the database, and when you modify an Entity Bean, the container will automatically synchronize the state of the Entity Bean with the database.
The difference between @ Entity and @ Table:
@ Entity indicates that the class is an entity class and uses the default orm rule, that is, the class name is the table name in the database table, and the class field name is the field name in the table.
If you want to change this default orm rule, use @ Table to change the mapping rule between the class name and the table name in the database, and @ Column to change the mapping rule between the field name in class and the field name in db.
The above is the content of this article on "how to use @ Entity and @ Table annotations". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.