Many-to-Many 多对多的映射可以使用一组Java集合不包含任何重复的元素来实现。我们已经看到了Hibernate如何设置映射集合。
集(SET)被映射到与映射表中<set>元素,并以java.util.HashSet初始化。您可以使用Set集合在类中时,集合不需要重复的元素。
在现实生活中多对多的例子挺多的。
例如:学生与老师,订单与商品等
下面我就以员工和项目来做例子,一个员工可以做多个项目,一个项目可以被多个员工做。
1.准备JavaBean
项目·实体·类
public class Project {
private Integer proid;
private String proname;
public Integer getProid() {
return proid;
}
public void setProid(Integer proid) {
this.proid = proid;
}
public String getProname() {
return proname;
}
public void setProname(String proname) {
this.proname = proname;
}
}
员工实体类(员工实体类中需要植入一个项目集合。)
public class Employee {
private Integer empid;
private String empname;
//植入项目集合 一个员工可以参与多个项目
private Set<Project> projects=new HashSet<Project>();
public Integer getEmpid() {
return empid;
}
public void setEmpid(Integer empid) {
this.empid = empid;
}
public String getEmpname() {
return empname;
}
public void setEmpname(String empname) {
this.empname = empname;
}
public Set<Project> getProjects() {
return projects;
}
public void setProjects(Set<Project> projects) {
this.projects = projects;
}
}
2.持久化配置文件
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.day04mapping.manytomany.entity">
<!--表名称-->
<class name="Employee" table="EMPLOYEE" schema="root">
<!--列名-->
<id name="empid" column="EMPID">
<!--主键生成的策略 native:自动生成主键字段-->
<generator class="native"></generator>
</id>
<property name="empname" column="EMPNAME"></property>
<!--单相关联-->
<!--植入set 标签 table 中间表的表名 -->
<set name="projects" table="PROEMP">
<!--column 多的一方表的外键列-->
<key column="REMPID"></key>
<!--column 中间表的另外一个外键列-->
<many-to-many column="RPROID" class="Project"></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.day04mapping.manytomany.entity">
<!--表名称-->
<class name="Project" table="PROJECT" schema="root">
<!--列名-->
<id name="proid" column="PROID">
<!--主键生成的策略 native:自动生成主键字段-->
<generator class="native"></generator>
</id>
<property name="proname" column="PRONAME"></property>
</class>
</hibernate-mapping>
3.hibernate.cfg.xml主配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 指定数据库所用到的驱动 -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<!-- 指定数据库链接的url,hibernate链接的数据库名 -->
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<!-- 指定连接数据库的用户名 -->
<property name="connection.username">root</property>
<!-- 指定连接数据库的用户口令 -->
<property name="connection.password">root</property>
<!-- Enable Hibernate‘s automatic session context management -->
<property name="current_session_context_class">thread</property>
<!--格式化sql -->
<property name="format_sql ">true</property>
<!-- 打印sql 控制台-->
<property name="show_sql">true</property>
<!-- 指定数据库方言 -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- 根据需要自动创建数据库表 -->
<property name="hbm2ddl.auto">update</property>
<!--多对多-->
<mapping resource="cn/day04mapping/manytomany/entity/Project.hbm.xml"></mapping>
<mapping resource="cn/day04mapping/manytomany/entity/Employee.hbm.xml"></mapping>
<!--延迟加载-->
<mapping resource="cn/day05lazy/entity/Student.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
4.书写测试类和方法
public class HibernateUtil {
static Configuration cfg;
static SessionFactory factory;
static ThreadLocal<Session> td;
//静态代码块
static {
cfg=new Configuration().configure();
factory=cfg.buildSessionFactory();
td=new ThreadLocal<Session>();
}
public static Session getSession(){
//看看有无线程变量
Session session = td.get();
if (session==null){
//线程中没有session对象 创建一个
session = factory.openSession();
td.set(session);
}
//没有和当前线程绑定
return session;
}
//关闭session
public static void closeSession(){
Session session = td.get();
td.set(null);
session.close();
}
}
//多对多单向关联的查询
@Test
public void test01(){
Session session = HibernateUtil.getSession(); //得到session
Employee employee = session.get(Employee.class, 1);
System.out.println(employee.getEmpname());
System.out.println("=============");
for (Project project:employee.getProjects()){
System.out.println(project.getProname());
}
}
//多对多单向关联的添加
@Test
public void test03(){
Session session = HibernateUtil.getSession();
//开启事务
Transaction transaction = session.beginTransaction();
Employee employee=new Employee();
employee.setEmpname("微冷的雨");
Project project=new Project();
project.setProname("开发部");
Project project1=new Project();
project1.setProname("研发部");
employee.getProjects().add(project);
employee.getProjects().add(project1);
//保存数据
session.save(employee);
session.save(project);
session.save(project1);
//提交事务
transaction.commit();
session.close();
}