The
Mapping a one-to-many bidirectional association:
When an association is established between a class and a class, it is convenient to navigate from one object to another or to another set of objects associated with it.
Step one:
Note: hibernate requires that the property type be set to an interface type (for example: Java.util.Set) when defining a collection class attribute in a persisted class.
Pros: declaring an interface type can improve the transparency of persisted types, and when Hibernate calls Setemp (), the passed parameter is an instance of Hibernate's custom class for that Interface.
If you set setemp () to java.util.HashSet, you force hibernate to pass only instances of the HashSet class as Parameters.
Cases:
JavaBean
public classDepartment {PrivateInteger id; PrivateString name; private set<employee> employees=new hashset<employee>(); @Override publicString toString () {return"Department [id=" + ID + ", name=" + name + "]"; } //Get,set Method omitted .....}☆ defining the Employee class public classEmployee {PrivateInteger id; PrivateString name; PrivateDepartment dept; //Get,set Method omitted .....}
This improves the robustness of the program by avoiding null pointer exceptions thrown by the application to access a null-valued EMP collection , because the collection is not Null.
Step two:
Writing the Hbm.xml configuration file
1.
<! DOCTYPE hibernate-mapping public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > package = "entity" > <class name= " Dept "table=" Dept "> <id name=" did "type=" int. "column=" did "> <generator class = "native" > </generator> </id> <property name= "name" type= "strin G "column=" NAME "/> <set name=" set "cascade=" save-update "inverse=" true "> <key column=" deptNo "></key> <!--key: Here is a multi-party defined foreign key--<one-to-many class =" Em P "/> <!--the entity type of a party--</set> </class >
2.
<! DOCTYPE hibernate- mappingpublic "-//hibernate/hibernate mapping DTD 3.0//en" "/http WWW.HIBERNATE.ORG/DTD/HIBERNATE-MAPPING-3.0.DTD "> Package =" entity "> <class Name= "emp" table= "emp" > <id name= "eid" type= "int" column= "eid" > class= "native" > </generator> </id> <property name= "ename" type= "string" column= "ename"/> class= "Dept" column= "deptNo" ></many-to-one> </class> </ Hibernate-mapping>
The <set> element contains the following properties:
1) Name: Sets the property name of the persisted class to be mapped, here is the Set property of the Dept class.
2) Cascade: When the value is "save-update", it indicates cascading save and Update.
The <set> element also contains two child elements:<key> and the persisted class associated with the <one-to-many>,<one-to-many> element setting, where the Dept class,<key> Element
Sets the foreign key (that is, the foreign key defined by many parties) for the table corresponding to the persisted class that is Associated.
And Hibernate gets the above information from this configuration:
The *<set> element indicates the EMP collection: type Java.util.Set
The *<class> child element indicates a set of EMP objects stored in the EMP Collection.
The *<key> child element indicates that the EMP is referencing the Dept table by Deptno.
The *cascade property takes the value "save-update", which indicates that when you save the update dept, the data in the EMP collection is cascade saved or updated!
Step three:
The large configuration is as Follows:
<?xml version= ' 1.0 ' encoding= ' utf-8 '? ><! DOCTYPE hibernate-Configuration public"-//hibernate/hibernate Configuration DTD 3.0//en" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" & gt;true</property> <!--formatted display Sql--<!--<property name= "format_sql" >true</property> <!--automatically generate student table--<property name= "hbm2ddl.auto" >up date</property> <!--association Small configuration--<mapping resource= "entity/emp.hbm.xml"/> <mapp ing resource= "entity/dept.hbm.xml"/> <!--<mappingclass= "cn.happy.entity.Grade"/>-</session-factory> Step Four: Write the test class
public Static void one () { Dept de=new Dept (); EMP em=new emp (); = electronicutil.getsession (); = session.begintransaction (); De.setname ("operation and Maintenance department"); Em.setename ("fat brother"); Em.setdept (de); De.getset (). Add (em); Session.save (de); Tx.commit (); System.out.println ("success"); Session.close (); }
One-to-many Bidirectional Association complete!!!!!
Talking about cascade and inverse
1: first clarify the role of Cascade and Inverse:
Inverse: Determines whether changes to the collection in the object are reflected in the db, so inverse only works on the collection, and only One-to-many and Many-to-many are valid, because only those two relationships have a collection ( Many-to-one and one-to-one only contain a reference to each other).
Cascade: determines whether changes to the object are reflected in the db, so the Cascade works for all associated relationships.
2:inverse:inverse describes how an association between objects is Maintained.
Inverse: only the elements that exist in the collection Tag.
The function of inverse is to reflect the changes of the set in the object into the DB
The default value for inverse is: false, which indicates that the changes to the collection are reflected in the db, Inverse:false is the active side, and the active side is responsible for maintaining the association Relationship.
Inverse is true: indicates that the changed collection is not reflected in the Database.
One-to-many: The property is on more than one Side. The inverse property should be set on fewer parties: true, and many of the parties set to false, which indicates that the association is maintained by more than one party, if
To less maintenance, it will be inserted or deleted when one party to update the other side of each object has a relationship with the object, if handed over to the other side to maintain, it will greatly improve
The efficiency (because The relationship is in the more than one side, directly inserted or deleted just Fine)
many-to-many: properties exist in separate Tables. The inverse property defaults to false; in Many-to-many relationships, the inverse at both ends of the relationship cannot be set to false, and if both are set to false, the insert operation causes the two relationships to be inserted in the relational Table. Cannot both be set to true (no action will affect data in the relational Table)
, so either side is set to true and the other side is set to false;
No.2 many-to-many bidirectional correlation
Step one: creation of entity classes
public classEmployeeImplementsjava.io.Serializable {// fields PrivateInteger empid; PrivateString empname; Privateset<project> projects =NewHashset<project>();... Other content omitted}02. Create a project persistence class public classProjectImplementsjava.io.Serializable {PrivateInteger proid; PrivateString proname; PrivateSet<employee> employees =NewHashset<employee>();... Other content omitted}
Step two: Create a Hbm.xml file
? xml version= "1.0" encoding= "utf-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ HIBERNATE-MAPPING-3.0.DTD ">className= "cn.happy.daymanymany.Employee" table= "Employee" >Emploee<id name= "empid" type= "java.lang.Integer" > <column name= "empid"/> <generatorclass= "sequence" > <param name= "sequence" >SEQ_ID</param> </generator> </id& Gt <property name= "empname" type= "java.lang.String" > <column name= "empname" length= "not-"NULL= "true"/> </property> <set name= "projects" inverse= "true" table= "proemp" >Promp<key column= "rempid"/><!--table proemp foreign keys Rempid--<many-to-manyclass= "cn.happy.daymanymany.Project" column= "rproid"/> </set> </class><?xml version= "1.0" encoding= "utf-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ HIBERNATE-MAPPING-3.0.DTD ">className= "cn.happy.daymanymany.Project" table= "Project" > <id name= "proid" type= "java.lang.Integer" > <column name= "proid"/> <generatorclass= "sequence" > <param name= "sequence" >SEQ_ID</param> </generator> </i d> <property name= "proname" type= "java.lang.String" > <column name= "proname" length= "not-"NULL= "true"/> </property> <set name= "employees" table= "proemp" cascade= "save-update" > <key column= "rproid"/> <many-to-manyclass= "cn.happy.daymanymany.Employee" column= "rempid"/> </set> </class>To correlate a many-to-many relationship of two classes through the foreign key of the third table
Large configuration ibid: Step three: write a test class
public void addtest () { emp emp=new emp (); Emp.setempname ("great beauty"); Project Pro=new Project (); Pro.setpname ("central support place"); Pro.getemps (). Add (emp); Emp.getpros (). Add (pro); Session.save (pro); Session.save (emp); }
Hibernate's Associated Mappings