Java framework --- hibernate (one-to-many) ing, java --- hibernate
One-to-multiple relationships can be classified into one-way and two-way relationships.
One-to-multiple relationship
One way is to find the other party only from one side, usually from the master class to find the class (table) with the foreign key ). For example, a mother can have multiple children, and the child has the mother's primary key as the foreign key. The relationship between mothers and children is one-to-many. If you want to perform operations on the mother information and reflect the information on the child information, you can configure cascade = "all" on the collection attribute of the mother configuration file ", indicates cascade Update Configuration of associated entities.
"Master" end: Multiple ends
<? Xml version = "1.0"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
One end:
<?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">
One-to-multiple unidirectional ing: the multi-to-one ing is maintained at one end of the multi-node, and the one-to-many ing is maintained by one end.
"Master" side: one end maintains this relationship, and uses the set tag to add the field ClazzId to the t_student table for association.
One-to-Multiple Association ing utilizes the principle of multi-to-one association ing:
Multi-to-one association ing: Add a foreign key to multiple ends and point to one end.
One-to-Multiple Association ing: Add a foreign key to one end on multiple ends, and maintain a relationship that is directed to multiple
That is to say, the one-to-many and multi-to-one ing policies are the same, but the station angles are different.
Disadvantages of the relationship maintained at one end: If the clazzId field in the t_student table is set to non-empty, it cannot be saved.
One-to-multiple bidirectional
One-to-multiple bidirectional. Each Student has a clazz reference during bidirectional Association, so the clazz. hbm. xml file configuration is somewhat different.
<? Xml version = "1.0"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
Use <set> On a one-end set to add a foreign key to the target table to point to one-end.
Use the <allow-to-one> label on multiple ends