hibernate 中多对多关系对象集合的保存
多对多关系映射和一对多关系映射开发步骤差不多,
例子如下:员工和项目之间的关系,一个员工可以参与多个项目;一个项目可以有多个开发人员参与。因此是多对多的关系。
1 分析数据表
1.1)员工表
CREATE TABLE employee( id INT PRIMARY KEY AUTO_INCREMENT, ename VARCHAR(50) NOT NULL);
1.2)项目表
CREATE TABLE project( id INT PRIMARY KEY AUTO_INCREMENT, pname VARCHAR(100) NOT NULL, startDate DATE);
1.3)员工和项目关系表
CREATE TABLE emp_pro_relation(
eid INT,
pid INT,
FOREIGN KEY (eid) REFERENCES employee(id),
FOREIGN KEY (pid) REFERENCES project(id));
2 编写实体类
Employee类
public class Employee { private int id; private String ename; private Set<Project> projects = new HashSet<Project>(); public int getId() { return id; } public void setId(int id) { this.id = id; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public Set<Project> getProjects() { return projects; } public void setProjects(Set<Project> projects) { this.projects = projects; } }
Project类
public class Project { private int id; private String pname; private Date startDate = new Date(); private Set<Employee> employees = new HashSet<Employee>(); public int getId() { return id; } public void setId(int id) { this.id = id; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public Date getStartDate() { return startDate; } public void setStartDate(Date startDate) { this.startDate = startDate; } public Set<Employee> getEmployees() { return employees; } public void setEmployees(Set<Employee> employees) { this.employees = employees; } }
3 映射配置
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="com.baidu.entity"> <class name="Employee" table="employee"> <id name="id" column="id"> <generator class="native"/> </id> <property name="ename" column="ename"></property> <set name="projects" table="emp_pro_relation"> <key column="eid"></key> <many-to-many column="pid" 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="com.baidu.entity"> <class name="Project" table="project"> <id name="id" column="id"> <generator class="native"/> </id> <property name="pname" column="pname"></property> <set name="employees" table="emp_pro_relation" cascade="save-update"> <key column="pid"></key> <many-to-many column="eid" class="Employee"></many-to-many> </set> </class> </hibernate-mapping>
4 全局配置hibernate.cfg.xml
<hibernate-configuration> <session-factory> <!-- 1 数据库连接配置 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///day26?useSSL=true</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <!-- 2 其他设置 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- 3 映射文件加载 --> <mapping resource="com/huitong/entity/Project.hbm.xml"/> <mapping resource="com/huitong/entity/Employee.hbm.xml"/> </session-factory> </hibernate-configuration>
二、简单测试
public void fun1(){ Configuration configuration = new Configuration(); configuration.configure(); SessionFactory sessionFac = configuration.buildSessionFactory(); Session session = sessionFac.openSession(); Transaction bt = session.beginTransaction(); Employee emp1 = new Employee(); emp1.setEname("good1"); Employee emp2 = new Employee(); emp2.setEname("good2"); Employee emp3 = new Employee(); emp3.setEname("nice1"); Employee emp4 = new Employee(); emp4.setEname("nice2"); Project pro1 = new Project(); pro1.setPname("OA"); Project pro2 = new Project(); pro2.setPname("电商"); pro1.getEmployees().add(emp1); pro1.getEmployees().add(emp2); pro1.getEmployees().add(emp3); pro2.getEmployees().add(emp3); pro2.getEmployees().add(emp4); session.save(pro1); session.save(pro2); bt.commit(); session.close(); sessionFac.close(); }
需要注意的是:在使用一方保存另一方时,可能会有错误。此时需要在保存方配置cascade属性。
学习过程中,难免出错。如果您在阅读过程中遇到不太明白,或者有疑问。欢迎指正...联系邮箱crazyCodeLove@163.com
如果觉得有用,想赞助一下请移步赞助页面:赞助一下