Basic requirements: In a department (Department) there are multiple employees (employee), in line with the 1:n relationship.
In section 05, the most basic type of string is used. In this section, the user-defined department class and the employee class are used, but this is precisely the issue that needs to be discussed.
The focus of this section is on mapping of custom types and mappings of 1:n relationships.
It involves the <one-to-many> and <many-to-one> tags, grasping their essence to capture 4 concepts: Table, field (column), class, and property.
Department.java
Package Com.rk.hibernate.g_one2many;import java.util.set;public class Department{private int deptid;private String deptname;private set<employee> emps;public int Getdeptid () {return deptid;} public void Setdeptid (int deptid) {this.deptid = DeptID;} Public String Getdeptname () {return deptname;} public void Setdeptname (String deptname) {this.deptname = Deptname;} Public set<employee> Getemps () {return emps;} public void Setemps (set<employee> emps) {this.emps = Emps;}}
Department.hbm.xml
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd ">
Employee.java
Package Com.rk.hibernate.g_one2many;public class Employee{private int empid;private String empname;private double salary;private Department dept;public int getempid () {return empId;} public void setempid (int empId) {this.empid = empId;} Public String Getempname () {return empname;} public void Setempname (String empname) {this.empname = EmpName;} Public double getsalary () {return salary;} public void Setsalary (double salary) {this.salary = salary;} Public Department getdept () {return dept;} public void Setdept (Department dept) {this.dept = dept;}}
Employee.hbm.xml
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd ">
App.java
package com.rk.hibernate.g_one2many;import java.util.hashset;import java.util.set;import org.hibernate.session;import org.hibernate.sessionfactory;import org.hibernate.cfg.configuration; import org.junit.test;public class app1{private static sessionfactory sf;static{ Sf = new configuration (). Configure (). addclass (Department.class). addclass (Employee.class). Buildsessionfactory ();} Save "department to maintain department and employee relationships" @Testpublic void testsavedepartment () {session Session = sf.opensession (); session.begintransaction ();//Employee 1employee emp1 = new Employee (); Emp1.setempname ("Zhang San"); emp1.setsalary (1000);//employee 2employee emp2 = new employee (); Emp2.setempname ("John Doe"); emp2.setsalary (800);//Department department dept = new department (); Dept.setdeptname ("Development Department"); Set<employee> emps = new hashset<employee> (); Emps.add (EMP1); eMps.add (EMP2);d ept.setemps (emps);//Establish the relationship between department and employee on the department//Save//Note: All three are to be saved. If you save only the Dept object, Dept only maintains the relationship between the department and the employee,//hibernate will not save Emp1 and Emp2 Session.save (dept); Session.save (EMP1); Session.save (EMP2); Session.gettransaction (). commit (); Session.close (); System.out.println ("Execution is over!") ");} "Recommend this method" "employee to maintain the relationship between department and employee" @Testpublic void testsaveemployee () { Session session = sf.opensession (); session.begintransaction ();//Department department dept = new department ();d ept.setdeptname ("Office");//employee 1employee emp1 = new employee (); Emp1.setempname ("Xiaoming"); Emp1.setsalary (+); emp1.setdept (dept);//employee to maintain the relationship between department and employee//employee 2Employee emp2 = new employee (), Emp2.setempname ("Little Red"), Emp2.setsalary (emp2.setdept); The relationship between department and employee is maintained by the employee//Save//Department and employee relationship is 1:N, first save the party representing 1, and then save the party representing N Session.save (dept); Session.save (EMP1); Session.save (EMP2); Session.gettransaction (). commit (); Session.close (); System.Out.println ("Execution is over!") ");} @Testpublic void testgetdepartment () {session session = sf.opensession (); Session.begintransaction ();D epartment dept = (Department) session.get (Department.class, &NBSP;2); System.out.println (Dept.getdeptid ()); System.out.println (Dept.getdeptname ()); System.out.println (Dept.getemps ());// through departments to get employees, lazy loading session.gettransaction (). commit (); Session.close (); System.out.println ("Execution is over!") ");} @Testpublic void testgetemployee () {session session = sf.opensession (); Session.begintransaction (); employee emp = (Employee) session.get (employee.class, 3); System.out.println (Emp.getempid ()); System.out.println (Emp.getempname ()); System.out.println (Emp.getsalary ()); System.out.println (Emp.getdept (). Getdeptname ());// through employees, get departmental information, lazy load session.gettransaction (). commit (); Session.close (); System.out.println ("Execution is over!") ");}}
(06) Association mappings for custom types