One-to-Multiple Association ing (unidirectionalClasses ---ÀStudent):
T_classes Id | Name 1 |Shang xuexiang |
T_student Id | Name | classesid 1 | 10 | 1 2 |Zuer| 1 |
One-to-Multiple Association ing example uses the multi-to-one association ing principle
Multi-to-one association ing: Add a foreign key to multiple ends to point to one end, and maintain multiple links to one.
One-to-Multiple Association ing: Add a foreign key to one end at one end, and maintain a one-to-many relationship.
That is to say, the one-to-many and multi-to-one ing policies are the same, but they have different site angles.
<SetName="Student">Attribute name
< Key Column = "Classesid" > </ Key > In Student Generate a foreign key name
One-to-second class = " com. hibernate. student " /> name of the table to be added
</Set>
Save the student first, and then save the effect of the class: Save the student first, but insert the foreign key field in the student tableNull,Save the class and update the foreign key field corresponding to the student table. The disadvantage is that manyUpdateStatement (Set). If the foreign key is set to a non-null value, it will certainly fail to be saved.
Disadvantages of One-end relationship maintenance:
If Student table classesid the field is not empty, cannot be saved
Because not inStudentThis end maintains the relationship, soStudentI don't know which class it belongs, so I need to issue redundantUpdateStatement to update the link
Bidirectional:
courier-to-one name = " classes " column = " classesid " > courier-to-one >
Note:ColumnMust matchOne-to-leastInKeyOfColumnOtherwise, a new column is generated.
Save the class before saving the student.
To solve the problem above:
< Set Name = "Student" Inverse = "true" > Invalidate one end, maintain multiple ends, and flip the link, but insert the link field Null, Therefore, only one end can be added: first save Classes Set Classes Set Student, Save Student
key column = " classesid " > key >
One-to-second class = " com. hibernate. student " />
</Set>
Save in the direction of 1:
Classes classes = new classes ();
Student S1 = new student ();
S1.setclasses (classes );
Set students = new hashset ();
Students. Add (S1 );
Classes. setstudent (students );
Session. Save (classes );
Configuration Requirements:
set name = " student " inverse = " true "cascade =" all " >
key column = " classesid " > key >
One-to-second class = " com. hibernate. student " />
</Set>
No need to saveStudent
One-to-multiple bidirectional Association:
UseKeyAdd a foreign key to the peer table to point to one-end
Use at multiple endsAllow-to-one
Note:KeyThe foreign key field specified by the tag must beAllow-to-oneThe specified foreign key fields are the same; otherwise, the referenced field is incorrect.
If one-to-multiple associations are maintained on one end,HibernateWill issue redundantUpdateStatement, So we usually maintain the Association at multiple ends.
AboutInverseAttribute:
Inverse It is mainly used for one-to-many and multi-to-many bidirectional Association, Inverse Can be set to set tags Set On, default Inverse Is False, Therefore, we can maintain associations from one end and multiple ends. Inverse Is True, Then we can only maintain the association from one end.
Note:InverseAttribute only affects data storage, that is, persistence.
InverseAndCascade
InverseIs the control direction of The Link
CascadeIs a chain reaction in Operation
SaveClassesAccordingInverseSaveStudent, studentAccordingCascadeSaveClasses