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