Hibernate annotation and hibernate Annotation
1. Introduction to JPA annotations
Hibernate Annotation: A new way to replace the XML ing object relationship with annotations
JPA: Java Persistence API is part of the EJB3.0 specification, which specifically describes how to deal with Persistence specifications.
Common Methods:
• Use JPA standard annotations for object link ing
• Use Hibernate to perform persistence operations.
JPA annotation definitions are placed in the javax. persistence package.
Common annotations include:
• @ Entity
• @ Table
• @ Id
• @ Temporal
• @ Basic
• Column
• Optimistic lock @ Version in JPA annotations
A
Compile the entity test class:
2. Object relationship ing Based on JPA annotations
Applications of association ing in JPA Annotations:
1. One-to-many (@ onetoworkflow)
2. One-to-one (@ OneToOne)
3. many-to-many (@ ManyToMany)
One to multiple:
Category and Product are one-to-multiple relationships. Products and categories are multi-to-one.
First, configure one-to-many annotations under the Category class at one end,
MappedBy indicates that the maintenance relationship is reversed, which is equivalent to property_ref. The maintenance relationship is reversed and handed over to the other end to maintain the relationship.
Let's take a look at multiple terminals:
// Configure the link here and determine the link maintenance end and the maintained end. MappBy indicates that the link is maintained, and only the link has the right to update the foreign key. Here, we also note that the default onetoworkflow loading method is dependent on loading. When the last word in the Set link is "bytes", the load is lazy by default.
Here, JoinColum is set to set the foreign key name. Here, Product indicates the link maintenance end, and Category indicates the link maintenance end.
One to one:
2. One-to-one ing based on primary keys:
It is not often used. This means that two tables use the same primary key and use Hibernate to customize the primary key generation policy, which is implemented by @ GenericGenerator. Reference myfor.
One-to-one based on foreign keys is relatively easier
3. many-to-many:
Taking Student and Course as an example, the relationship between students and courses is many-to-many.
JoinTable: An intermediate table is created as an association between two tables. After configuring the Student end, the Course end configuration is simple. You can simply use @ ManyToMany.
Ing name query
• NameQueries
• NameQuery