We use annotations in hibernate, which can help us simplify hbm file configuration.
1.1. PO class annotation configuration
@Entity declares an entity br/>@Table to describe the class and table correspondence
br/>@Id to declare a primary key
@GenerateValue Use it to declare a primary key generation strategy
By default, it is equivalent to native
Selectable primary key generation strategy AUTO IDENTITY SEQUENCE br/>@Column to define the column
br/>Note: For all attributes in the PO class, if you do not write comments, they will also be generated in the table by default Corresponding column.
The name of the column is the name of the attribute
@Temporal to declare the date type
You can choose
TemporalType.DATA only has year, month and day
TemporalType.TIME only has hours, minutes and seconds
TemporalType.TIMESTAMP has year, month, day, hour, minute and second
We finally need in hibernate.cfg The annotation configuration reference in our class will take effect in the .xml file
Question :1. If our primary key generation strategy wants to use UUID type?
Question 2: If the attributes of the set class are not mapped in the table?
For the annotations about attribute configuration we explained above, we can also Use it in its corresponding getXxx method
1.2. One-to-many (many-to-one) br/>@OneToMany
@ManyToOne br/>Take Customer and Order as examples
Customer class
![](https://s1.51cto.com/images /blog/201909/11/b59a68c8de4de8cb8710f8269171bd84.jpg?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_naG3GVpo>ZWkFu)[Order Class
Example: Save the order when saving the customer
For this example, we need to configure the cascade operation in the Customer, save-update
The first way, you can use the annotations provided by JPA
! [](https://s1.51cto.com/images/blog/201909/11/ec7c490819f1ee0b05d9254677234e89.jpg?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10, ,type_ZmFuZ3poZW5naGVpdGk=)
The second way: You can use the annotations provided by hibernate
![](https://s1.51cto.com/images/blog/201909/11/638c1b9b568f73e22601a1b5e2979f29.jpg?x-oss -process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,co lor_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
The following is the sample code
![](https://s1.51cto.com/images/blog/201909/11/4cf89861366a5b5a9ee9859de ?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
Results after execution
![](https://s1. 51cto.com/images/blog/2020 There is no associated customer id, why?
Reason: We have configured mappedBy=”c” in the Customer, which means that the maintenance of the foreign key is maintained by the Order side, and the Customer is not maintained. At this time, when you save the customer, you can cascade to save the order. Yes, but the foreign key cannot be maintained, so we must add the order and customer relationship in the code.
![](https://s1.51cto.com/images/blog/201909/11/2139a0ee801f1251a2641e276cf3bd81.jpg?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10 ,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
Extension: About hibernate annotation @Cascade DELETE_ORPHAN obsolete
Use the following scheme to replace the outdated scheme
p>
Hibernate annotation development
WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4477 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC