码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate关联关系之多对多

时间:2017-12-30 17:07:37      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:多对多   http   src   指定   nat   font   int   inf   tab   

多对多映射关系

建立多对多单向关联关系

创建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     }

执行结果

技术分享图片

 

Hibernate关联关系之多对多

标签:多对多   http   src   指定   nat   font   int   inf   tab   

原文地址:https://www.cnblogs.com/liutao1122/p/8150063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!