我们拿工程和员工这个例子来说,一个工程可以被多个员工去完成,一个员工也可以同时去完成多个工程
我们先来看实体类:
首先是工程的实体类:
package cn.itcast.c_many2many;
import java.util.HashSet;
import java.util.Set;
/**
* 项目
*
* @author Jie.Yuan
*
*/
public class Project {
private int prj_id;
private String prj_name;
// 项目下的多个员工
private Set<Developer> developers = new HashSet<Developer>();
public int getPrj_id() {
return prj_id;
}
public void setPrj_id(int prjId) {
prj_id = prjId;
}
public String getPrj_name() {
return prj_name;
}
public void setPrj_name(String prjName) {
prj_name = prjName;
}
public Set<Developer> getDevelopers() {
return developers;
}
public void setDevelopers(Set<Developer> developers) {
this.developers = developers;
}
}
然后看员工的实体类:
package cn.itcast.c_many2many;
import java.util.HashSet;
import java.util.Set;
/**
* 开发人员
*
* @author Jie.Yuan
*
*/
public class Developer {
private int d_id;
private String d_name;
// 开发人员,参数的多个项目
private Set<Project> projects = new HashSet<Project>();
public int getD_id() {
return d_id;
}
public void setD_id(int dId) {
d_id = dId;
}
public String getD_name() {
return d_name;
}
public void setD_name(String dName) {
d_name = dName;
}
public Set<Project> getProjects() {
return projects;
}
public void setProjects(Set<Project> projects) {
this.projects = projects;
}
}
下面是映射文件的配置:
工程的映射文件
<?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.itcast.c_many2many">
<class name="Project" table="t_project">
<id name="prj_id">
<generator class="native"></generator>
</id>
<property name="prj_name" length="20"></property>
<!--
多对多映射:
1. 映射的集合属性: “developers”
2. 集合属性,对应的中间表: “t_relation”
3. 外键字段: prjId
4. 外键字段,对应的中间表字段: did
5. 集合属性元素的类型
-->
<set name="developers" table="t_relation" cascade="save-update" inverse="true">
<key column="prjId"></key>
<many-to-many column="did" class="Developer"></many-to-many>
</set>
</class>
</hibernate-mapping>
员工的映射文件:
<?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.itcast.c_many2many">
<class name="Developer" table="t_developer">
<id name="d_id">
<generator class="native"></generator>
</id>
<property name="d_name" length="20"></property>
<!--
多对多映射配置: 员工方
name 指定映射的集合属性
table 集合属性对应的中间表
key 指定中间表的外键字段(引用当前表t_developer主键的外键字段)
many-to-many
column 指定外键字段对应的项目字段
class 集合元素的类型
-->
<set name="projects" table="t_relation">
<key column="did"></key>
<many-to-many column="prjId" class="Project"></many-to-many>
</set>
</class>
</hibernate-mapping>
这样,一个多对多的关联关系我们就配好了
下面我们来写一个新增的测试看一下:
package cn.itcast.c_many2many;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.junit.Test;
import cn.itcast.a_collection.User;
public class App1_save {
private static SessionFactory sf;
static {
sf = new Configuration()
.configure()
.addClass(Project.class)
.addClass(Developer.class) // 测试时候使用
.buildSessionFactory();
}
// 1. 多对多,保存 【只能通过一方维护另外一方,不能重复维护!】
@Test
public void save() {
Session session = sf.openSession();
session.beginTransaction();
/*
* 模拟数据:
电商系统(曹吉,王春)
OA系统(王春,老张)
*/
// 创建项目对象
Project prj_ds = new Project();
prj_ds.setPrj_name("电商系统");
Project prj_oa = new Project();
prj_oa.setPrj_name("OA系统");
// 创建员工对象
Developer dev_cj = new Developer();
dev_cj.setD_name("曹吉");
Developer dev_wc = new Developer();
dev_wc.setD_name("王春");
Developer dev_lz = new Developer();
dev_lz.setD_name("老张");
// 关系 【项目方】
prj_ds.getDevelopers().add(dev_cj);
prj_ds.getDevelopers().add(dev_wc); // 电商系统(曹吉,王春)
prj_oa.getDevelopers().add(dev_wc);
prj_oa.getDevelopers().add(dev_lz); // OA系统(王春,老张)
// 保存
// session.save(dev_cj);
// session.save(dev_wc);
// session.save(dev_lz);
session.save(prj_ds);
session.save(prj_oa); // 必须要设置级联保存
session.getTransaction().commit();
session.close();
}
}
到此一个完整的hibernate的多对多的关联映射已经完成。