Hibernate annotations (2): association relationship mapping annotations

Association relationship mapping annotation
The following items are modified on the basis of the previous corresponding code. What these projects need to do are:
 Delete the mapping file
 Register the entity class in hibernate.cfg.xml
 The important position of the annotation is on the associated attribute in the entity class. Here is the embodiment of the relationship.

(1) One-to-many one-way association

Example: The item annotation_one2many_s
@OneToMany indicates a one-to-many relationship.
@JoinColumn indicates the foreign key associated with the property.

 targetEntity: Indicates the class associated with the property.
 cascade: Specify the cascade type. It is an array, using multiple cascades, you can use { } assignment. Its value is the Cascade constant:

For the multiple parties of the one-to-many one-way association relationship, since it does not have the ability to maintain the association relationship, that is, there is no object of one party as an attribute, so there is no need to set the association-related annotations here.

(2) One-to-many bidirectional association

Example: The project annotation_one2many_d
adds the mappedBy attribute.

Usage of the mappedBy attribute:
 This attribute is related to the maintenance right of the association relationship.
 The property should be placed on the party that waives the right to maintain.
 The attribute value is the associated attribute of the other party, indicating that it will be responsible for the future association relationship.
 Using the annotation of this property, you don't need to and can't set the cascade property anymore.
 This property may only be used in bidirectional associations.
 After using this property, you can no longer use the @JoinColumn annotation. Because the @JoinColumn annotation indicates which field in the DB will be associated with the attribute it annotated after being set by the set method in the future.
The mappedBy attribute indicates that the associated attribute of the current annotation has given up the right to maintain, and even if the set method is executed to set the value, it will not be written to the DB.
It is precisely because the right to maintain is given up that it has nothing to do with the DB, so it is meaningless to set the annotation of the mappedBy attribute and then set the cascade.
One is to make the annotated person related to the DB, the other is to make the annotated person give up the relationship with the DB, they are contradictory.

(3) Self-Association

(4) Many-to-one unidirectional association

 

 

(5) Many-to-many one-way association

Example: annotation_many2many_s
many-to-many associations are annotated with @ManyToMany. It will automatically generate an intermediate table whose name is the union of the mapping table names of the two associated objects: table1_table2. The table contains two fields, and the field names are also related to the table name. The field names are: table 1_id and table 2_id. Of course, the default table name and field name can be modified through @JoinTable (not studied).

(6) Many-to-many bidirectional association

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326411391&siteId=291194637