one-to-many(一对多)和many-to-one(多对一)双向关联
假设部门与员工是一对多关系,反过来员工与部门就是多对一关系。
Dept.java类
1 public class Dept implements java.io.Serializable { 2 3 // Fields 4 5 private Integer deptId; 6 private String deptName; 7 private Set<Emp> emps=new HashSet<Emp>(); 8 9 // Constructors 10 11 /** default constructor */ 12 public Dept() { 13 } 14 15 /** full constructor */ 16 public Dept(String deptName) { 17 this.deptName = deptName; 18 } 19 20 // Property accessors 21 22 public Integer getDeptId() { 23 return this.deptId; 24 } 25 26 public void setDeptId(Integer deptId) { 27 this.deptId = deptId; 28 } 29 30 public String getDeptName() { 31 return this.deptName; 32 } 33 34 public void setDeptName(String deptName) { 35 this.deptName = deptName; 36 } 37 38 public Set<Emp> getEmps() { 39 return emps; 40 } 41 42 public void setEmps(Set<Emp> emps) { 43 this.emps = emps; 44 } 45 46 }
Emp.java类
1 public class Emp implements java.io.Serializable { 2 3 // Fields 4 5 private Integer empNo; 6 private String empName; 7 private Date empBrithday; 8 private Dept dept; 9 10 // Constructors 11 12 /** default constructor */ 13 public Emp() { 14 } 15 16 /** full constructor */ 17 public Emp(String empName, Date empBrithday) { 18 this.empName = empName; 19 this.empBrithday = empBrithday; 20 } 21 22 // Property accessors 23 24 public Integer getEmpNo() { 25 return this.empNo; 26 } 27 28 public void setEmpNo(Integer empNo) { 29 this.empNo = empNo; 30 } 31 32 public String getEmpName() { 33 return this.empName; 34 } 35 36 public void setEmpName(String empName) { 37 this.empName = empName; 38 } 39 40 public Date getEmpBrithday() { 41 return this.empBrithday; 42 } 43 44 public void setEmpBrithday(Date empBrithday) { 45 this.empBrithday = empBrithday; 46 } 47 48 public Dept getDept() { 49 return dept; 50 } 51 52 public void setDept(Dept dept) { 53 this.dept = dept; 54 } 55 56 }
Dept.hbm.xml
1 <hibernate-mapping> 2 <class name="com.db.entity.Dept" table="dept" catalog="mydb"> 3 <id name="deptId" type="java.lang.Integer"> 4 <column name="deptId" /> 5 <generator class="native" /> 6 </id> 7 <property name="deptName" type="java.lang.String"> 8 <column name="deptName" length="32" /> 9 </property> 10 <set name="emps" inverse="true" cascade="all"> 11 <key column="deptId" not-null="true" /> 12 <one-to-many class="com.db.entity.Emp" /> 13 </set> 14 </class> 15 </hibernate-mapping>
Emp.hbm.xml
1 <hibernate-mapping> 2 <class name="com.db.entity.Emp" table="emp" catalog="mydb"> 3 <id name="empNo" type="java.lang.Integer"> 4 <column name="empNo" /> 5 <generator class="native" /> 6 </id> 7 <property name="empName" type="java.lang.String"> 8 <column name="empName" length="20" /> 9 </property> 10 <property name="empBrithday" type="java.util.Date"> 11 <column name="empBrithday"/> 12 </property> 13 <many-to-one name="dept" column="deptId" class="com.db.entity.Dept" not-null="true" fetch="select" cascade="save-update,delete"/> 14 </class> 15 </hibernate-mapping>
hibernate.cfg.xml
1 <hibernate-configuration> 2 3 <session-factory> 4 <property name="dialect"> 5 org.hibernate.dialect.MySQLDialect 6 </property> 7 <property name="connection.url"> 8 jdbc:mysql://localhost:3306/mydb 9 </property> 10 <property name="connection.username">root</property> 11 <property name="connection.password">123456</property> 12 <property name="connection.driver_class"> 13 com.mysql.jdbc.Driver 14 </property> 15 <property name="myeclipse.connection.profile"> 16 MyDBAccount 17 </property> 18 <mapping resource="com/db/entity/Dept.hbm.xml" /> 19 <mapping resource="com/db/entity/Emp.hbm.xml" /> 20 </session-factory> 21 22 </hibernate-configuration>
测试用例
1 public class TestOneMany { 2 3 public static void main(String[] args) throws ParseException { 4 // TODO Auto-generated method stub 5 6 Session session=HibernateSessionFactory.getSession(); 7 Dept dept1=new Dept(); 8 dept1.setDeptName("开发部"); 9 Emp emp1=new Emp(); 10 emp1.setEmpName("王洋"); 11 String brithString="1999-03-05"; 12 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 13 Date brithday=sdf.parse(brithString); 14 emp1.setEmpBrithday(brithday); 15 Emp emp2=new Emp(); 16 emp2.setEmpName("李林"); 17 brithString="2005-02-07"; 18 brithday=sdf.parse(brithString); 19 emp2.setEmpBrithday(brithday); 20 //把emp和dpt分别添加到对方的实力对象中 21 dept1.getEmps().add(emp1); 22 dept1.getEmps().add(emp2); 23 emp1.setDept(dept1); 24 emp2.setDept(dept1); 25 session.beginTransaction(); 26 session.save(dept1); 27 session.getTransaction().commit(); 28 } 29 30 }
注意:
在一的set配置中声明inverse=“true”,意味着Dept不在作为主控方,将关联关系的维护工作交给关联对象Emp来完成。在保存dept1对象的时不在关心Emp类中dept属性对应的deptId列,必须由Emp自己去维护,即设置emp.setDept(dept)
上述操作完成的sql语句是:
Hibernate: insert into mydb.dept (deptName) values (?)
Hibernate: insert into mydb.emp (empName, empBrithday, deptId) values (?, ?, ?)
Hibernate: insert into mydb.emp (empName, empBrithday, deptId) values (?, ?, ?)