A one-to-many relationship can be divided into unidirectional and bidirectional.
One-to-many relationship unidirectional
One-way is to find the other side only from the side, usually from the master class to find the Class (table) that owns the foreign key. For example, a mother can have multiple children, and the child has a mother's primary key as a foreign key. The relationship between mother and child is a one-to-many relationship. If you want to act on the mother's information and also respond to the child's information, you can configure cascade= "All" on the collection properties of the mother's class profile, which means that the associated entity is configured for cascading updates.
"Main" side: one end of the many
<?XML version= "1.0"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/ Dtd/hibernate-mapping-3.0.dtd "><hibernate-mapping Package= "Com.java1234.model"> <classname= "Student"Table= "T_student"> <IDname= "id"column= "Stuid"> <Generatorclass= "Native"></Generator> </ID> <!--multiple end//column aliases in data cascade indicates that cascading relationships are often used in many-to-one, and many-to-many mappings - < Propertyname= "Name"></ Property> <Many-to-onename= "C"class= "Com.java1234.model.Clazz"column= "Clazzid"></Many-to-one> </class></hibernate-mapping>
One end of the:
<?XML version= "1.0"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/ Dtd/hibernate-mapping-3.0.dtd "><hibernate-mapping Package= "Com.java1234.model"> <classname= "Clazz"Table= "T_clazz"> <IDname= "id" > <Generatorclass= "Native"></Generator> </ID> < Propertyname= "Name"column= "Clazzname"></ Property> </class></hibernate-mapping>
One-to-many unidirectional mappings: many-to-one mappings are maintained at a multiple end, and one-to-many relationship mappings maintain this relationship at the end.
"Main" side: one end to maintain this relationship, with the set tag in the T_student table to add field Clazzid to associate.
A one-to-many correlation mapping utilizes a multi-pair correlation mapping principle:
Many-to-one association mappings: At one end of a multi-side join the foreign key point to the end, he maintained the relationship is more point to a
One-to-many association mappings: at the end of a multi-side join the foreign key to point to a side, it maintains the relationship is a point to multi-
That is, a one-to-many and a-to-many mapping strategy is the same, only the angle of the station is different.
Disadvantages of maintaining relationships at one end: If the Clazzid field inside the T_student table is set to non-empty, it cannot be saved.
One-to-many bidirectional
One-to-many bidirectional. Each student has a clazz reference in a bidirectional association, so there are some differences when configuring Clazz.hbm.xml files
<?XML version= "1.0"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/ Dtd/hibernate-mapping-3.0.dtd "><hibernate-mapping Package= "Com.java1234.model"> <classname= "Clazz"Table= "T_clazz"> <IDname= "id"column= "Clazzid"> <Generatorclass= "Native"></Generator> </ID> < Propertyname= "Name"column= "ClassName"></ Property> <Setname= "Students"Cascade= "All"Inverse= "true"> <!--Cascade has five options: All, delete, None,save-update,delete-orphan; All: Associated operations are performed in all cases. None: Associated operations are not performed in all cases. This is the default value. Save-update: The associated operation is performed when Save/update/saveorupdate is executed. Delete: The associated operation is performed when the delete is executed. Delete-orphan: When Save/update/saveorupdate, the equivalent of Save-update, when the deletion operation, the equivalent of delete; - <Keycolumn= "Clazzid"></Key> <One-to-manyclass= "Com.java1234.model.Student" /> </Set> </class></hibernate-mapping>
Use <set> on the one by one-end collection, and add a foreign key to the other table to point to one end.
Use <many-to-one> tags at one end of a lot
Java Framework---Hibernate (one-to-many) mapping relationship