多对多映射关系
建立多对多单向关联关系
创建Employee类:
1 public class Employee { 2 private Integer empid; //员工编号 3 private String empname; //员工姓名 4 private Set<Project> projects = new HashSet<Project>(); //植入项目集合 5 }
Employee.hbm.xml:
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping package="cn.hibernate.day04mapping.manytomany"> 6 <!--实体 name=实体端的内容 column=DB端的内容--> 7 <class name="Employee" table="EMPLOYEE" schema="liutao"> 8 <!--和底层数据表对应的主键 业务意义--> 9 <id name="empid" column="EMPNO"> 10 <generator class="native"></generator> 11 </id> 12 <property name="empname" column="EMPNAME"></property> 13 <set name="projects" table="REMPPRO"> 14 <key column="REMPID"></key> 15 <many-to-many class="Project" column="RPROID"></many-to-many> 16 </set> 17 </class> 18 </hibernate-mapping>
属性详解:
name=类中植入的集合名称 table=中间表名称
<key column=“本类在中间表的外键”></key>
<many-to-many class="映射类" column="映射类在中间表的外键"></many-to-many>
创建Project类 :
1 public class Project { 2 private Integer proid; 3 private String proname; 4 }
Project.hbm.xml
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping package="cn.hibernate.day04mapping.manytomany"> 6 <!--实体 name=实体端的内容 column=DB端的内容--> 7 <class name="Project" table="PROJECT" schema="liutao"> 8 <!--和底层数据表对应的主键 业务意义--> 9 <id name="proid" column="PROID"> 10 <generator class="native"></generator> 11 </id> 12 <property name="proname" column="PRONAME"></property> 13 </class> 14 </hibernate-mapping>
测试方法
1 //多对多 单向关联 添加一个员工对应多个工程 2 @Test 3 public void t2(){ 4 Session session = HibernateUtil.getSession(); 5 Transaction tx=session.beginTransaction(); 6 Employee emp1=new Employee(); 7 emp1.setEmpname("曹梅花"); 8 Project pro1=new Project(); 9 pro1.setProname("海淀花园"); 10 Project pro2=new Project(); 11 pro2.setProname("三国统治"); 12 13 emp1.getProjects().add(pro1); 14 emp1.getProjects().add(pro2); 15 16 session.save(emp1); 17 session.save(pro1); 18 session.save(pro2); 19 tx.commit(); 20 session.close(); 21 }
表结构
建立多对多双向关联关系
Employee.hbm.xml
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping package="cn.hibernate.day04mapping.manytomany"> 6 <!--实体 name=实体端的内容 column=DB端的内容--> 7 <class name="Employee" table="EMPLOYEE" schema="liutao"> 8 <!--和底层数据表对应的主键 业务意义--> 9 <id name="empid" column="EMPNO"> 10 <generator class="native"></generator> 11 </id> 12 <property name="empname" column="EMPNAME"></property> 13 <set name="projects" table="REMPPRO" lazy="true"> 14 <key column="REMPID"></key> 15 <many-to-many class="Project" column="RPROID"></many-to-many> 16 </set> 17 </class> 18 </hibernate-mapping>
Project.hbm.xml
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping package="cn.hibernate.day04mapping.manytomany"> 6 <!--实体 name=实体端的内容 column=DB端的内容--> 7 <class name="Project" table="PROJECT" schema="liutao"> 8 <!--和底层数据表对应的主键 业务意义--> 9 <id name="proid" column="PROID"> 10 <generator class="native"></generator> 11 </id> 12 <property name="proname" column="PRONAME"></property> 13 <set name="employees" table="REMPPRO" lazy="true"> 14 <key column="RPROID"></key> 15 <many-to-many class="Employee" column="REMPID"></many-to-many> 16 </set> 17 </class> 18 </hibernate-mapping>
测试方法
1 //多对多双向 查询指定工程参与人员 2 //查询员工所属工程 3 @Test 4 public void t3(){ 5 Session session = HibernateUtil.getSession(); 6 Transaction tx=session.beginTransaction(); 7 Project project = session.load(Project.class, 42); 8 System.out.println(project.getProname()); 9 for (Employee item:project.getEmployees() 10 ) { 11 System.out.println(item.getEmpname()); 12 } 13 tx.commit(); 14 session.close(); 15 }
执行结果