多对多映射关系
建立多对多单向关联关系
创建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 }
执行结果
建立多对多双向关联 拆成两个多对一
Employee
1 //员工实体 2 public class Employee { 3 private Integer empid; //员工编号 4 private String empname; //员工姓名 5 private Set<Project> projects = new HashSet<Project>(); //植入项目集合 6 }
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.manytomanydoublebyonetomany"> 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 </-mapping>
ProEmp
1 public class ProEmp { 2 private Integer id; 3 private Project pro; 4 private Employee emp; 5 }
ProEmp.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.manytomanydoublebyonetomany"> 6 <class name="ProEmp" table="PROEMP"> 7 <id name="id" column="ID"> 8 <generator class="native"></generator> 9 </id> 10 <many-to-one name="emp" class="Employee"> 11 <column name="empid"></column> 12 </many-to-one> 13 <many-to-one name="pro" class="Project"> 14 <column name="proid"></column> 15 </many-to-one> 16 </class> 17 </hibernate-mapping>
Project
1 public class Project { 2 private Integer proid; 3 private String proname; 4 private Set<Employee> employees=new HashSet<Employee>(); 5 }
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.manytomanydoublebyonetomany"> 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 @Test 3 public void t4(){ 4 Session session = HibernateUtil.getSession(); 5 Transaction tx=session.beginTransaction(); 6 //创建EMP对象 7 Employee emp=new Employee(); 8 emp.setEmpname("李小龙"); 9 10 //创建Pro对象 11 Project pro=new Project(); 12 pro.setProname("海淀花园"); 13 14 //创建ProEmp对象 15 ProEmp proemp=new ProEmp(); 16 proemp.setEmp(emp); 17 proemp.setPro(pro); 18 19 session.save(emp); 20 session.save(pro); 21 session.save(proemp); 22 // 事务提交 23 tx.commit(); 24 session.close(); 25 System.out.println("成功"); 26 }