First, Introduction
First of all, correlation is an object-oriented analysis, the most important part of object-oriented design,JPA is fully mapped to simplify data persistence to data, and Hibernate like, JPA Are divided into two types, one is one-way, and the other is two-way association:
One-way Association: Only one-way access to the association end, such as: we can only through a semester to access the course of the semester, but not through the course to visit the course of the semester, this association is a one-way association.
Bidirectional Association: Both ends of the association can be accessed from each other. Compared to the class can access students, students can also access the class, this association is a two-way association.
Association relationships can be divided into the following categories:
Unidirectional:1-1;1-n; N-1; N-n
bidirectional:1-1;1-n; N-n; no two-way correlation. N-1 , because in a bidirectional relationship 1-n and the N-1 is exactly the same.
Second, the association attribute
Whether a one-way or two-way association requires that the attributes of the associated relationship be recorded using the @ManyToOne annotation decoration in the entity of N , using the Manytoone the attributes commonly used for annotations are:
1 , Cascade : Specifies what cascade strategy to use for the associated entity, with four commonly used attributes:
( 1 ) Cascadetype.all: Cascade all persisted operations to the associated entity
( 2 ) Cascadetype.merge : The Merge operations are cascaded to the associated entity
( 3 ) cascadetype.persist: will be persist operations are cascaded to the associated entity
( 4 ) Cascadetype.refresh: will be Refresh operations are cascaded to the associated entity
( 5 ) Cascadetype.remove: will be Remove operations are cascaded to the associated entity
2 , Fetch : Specifies the crawl strategy when fetching associated entities, with the following two types of properties commonly used:
( 1 ) Fetchtype.eager: Fetch The associated entity immediately when fetching the entity
( 2 ) Fetchtype.lazy: when fetching entities, the demo fetches the associated entities and then crawls when the associated entity is actually called
3 , targetentity : Specifies the class name of the associated entity, and for most association relationships, JPA It is possible to determine the type of the associated entity by reflection, so this property is not necessary, but there are special cases, such as one-to-many, many-to-many, if the entity does not have generic information Set collection to record the associated entity, you must specify targetentity Properties
Third, mapping database properties
When mapping entities, there are N At one end, when you persist the attributes of an entity to a database, you need to specify the properties of the attribute in the database, and the annotations used here are @JoinColumn , where the commonly used attributes are:
1 , name : Specifies the column name of the foreign key column for this property
2 , Nullable : Specifies whether the column is allowed to be empty, which is nullable by default
3 , Table : Specifies the table name of the data table in which the column resides, by default in a database table that is mapped by one of the many end entities
4 , Unique : Specifies whether the column is a unique constraint.
just contact to know not much about JPA The associated mappings and the attributes written in this blog are detailed in the next example of a single-bidirectional association map.
An overview of the JPA Entity Association relationship Mapping