In contrast to the many-to-one relationship above, this illustrates the next-to-many correlation.
In the above, we describe a many-to-one relationship, which is a many-to-one relationship in a relational database, and it is a pair-to-many relationship. However, this is not enough, Hibernate is an object-oriented structure, and in hibernate it is still a many-to-one relationship, but there is not a pair, so we need to add a one-to-many relationship.
Department entity class: Department.java
Package Cn.itcast.hibernate.domain;import Java.util.set;public class Department { private int id; private String name; Private set<employee> Emps; Public set<employee> Getemps () {return emps;} public void Setemps (set<employee> emps) {this.emps = Emps;} public int getId () { return ID; } public void setId (int id) { this.id = ID; } Public String GetName () { return name; } public void SetName (String name) { this.name = name; } }
Department Mapping relationship: Department.hbm.xml
<?xml version= "1.0"?> <! DOCTYPE hibernate-mapping public "-//hibernate/hibernate mapping DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
We need to note that in the Department entity class, we add a set set property, and in the mapping file we also add a set property, which is to map one-to-many relationships.In the set tag in the mapping file, we define a key tag, which is a foreign key and must be the same as the foreign key of the employee class.
Employee Entity class: Employee.java
Package Cn.itcast.hibernate.domain;public class Employee { private int id; private String name; Private Department depart; public int getId () { return ID; } public void setId (int id) { this.id = ID; } Public String GetName () { return name; } public void SetName (String name) { this.name = name; } Public Department Getdepart () { return depart; } public void Setdepart (Department depart) { This.depart = depart; } }
Employee Mapping Relationship: Employee.hbm.xml<?xml version= "1.0"?> <! DOCTYPE hibernate-mapping public "-//hibernate/hibernate mapping DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
Next, we write a test class: Many2one.javapublic class Many2one {public static void main (string[] arg) {Department depart = Add (); Query (Depart.getid ()); } static Department Add () {Session s = null; Transaction tx = NULL; try{Department depart = new Department (); Depart.setname ("Depart name"); Employee EMP1 = new Employee (); Emp1.setdepart (depart); Establish an association relationship of two objects emp1.setname ("emp name"); Employee EMP2 = new Employee (); Emp2.setdepart (depart); Establish an association relationship of two objects emp2.setname ("emp name"); s = hibernateutil.getsession (); tx = S.begintransaction (); S.save (depart); S.save (EMP1); The EMP object and depart object are saved in different order and there will be some difference s.save (EMP2); If an employee (EMP) is inserted after the department (depart), it generates three database statements//Because the EMP's depart_id field is not inserted after both insertions, so there is a last statement to insert Tx.commit (); return depart; }finally{if (s!=null) {s.close (); }}} static Department query (int departid) {Session s = null; Transaction tx = NULL; try{s = hibernateutil.getsession (); tx = S.begintransaction (); Department depart = (Department) s.get (Department.class, Departid); Query System.out.println by ID ("EMP size:" +depart.getemps (). Size ()); Hibernate.initialize (Depart.getemps ()); Tx.commit (); return depart; }finally{if (s!=null) {s.close (); } } } }
In the main function, we add two data and then query the department ID to get two results.
"SSH Three frames" Hibernate Foundation seventh: operation of a one-to-many association relationship