建立多对多双向关联关系
1.创建Employee持久化类
public class Employee implements java.io.Serializable {
// Fields
private Integer empid;
private String empname;
private Set<Project> projects = new HashSet<Project>();
..........
}
===================================================
2.创建Project持久化类
public class Project implements java.io.Serializable {
private Integer proid;
private String proname;
private Set<Employee> employees = new HashSet<Employee>();
...............
}
===================================================
3.创建Employee.hbm.xml文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.happy.daymanymany.Employee" table="EMPLOYEE">
<id name="empid" type="java.lang.Integer">
<column name="EMPID"/>
<generator class="sequence">
<param name="sequence">SEQ_ID</param>
</generator>
</id>
<property name="empname" type="java.lang.String">
<column name="EMPNAME" length="32" not-null="true" />
</property>
<set name="projects" inverse="true" table="PROEMP">
<key column="REMPID"/><!-- 表PROEMP的外键REMPID -->
<many-to-many class="cn.happy.daymanymany.Project" column="RPROID" />
</set>
</class>
</hibernate-mapping>
======================================================================
4.创建Project.hbm.xml文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.happy.daymanymany.Project" table="PROJECT">
<id name="proid" type="java.lang.Integer">
<column name="PROID"/>
<generator class="sequence">
<param name="sequence">SEQ_ID</param>
</generator>
</id>
<property name="proname" type="java.lang.String">
<column name="PRONAME" length="32" not-null="true" />
</property>
<set name="employees" table="PROEMP" cascade="save-update">
<key column="RPROID" />
<many-to-many class="cn.happy.daymanymany.Employee" column="REMPID" />
</set>
</class>
</hibernate-mapping>
===================================================================
05.hibernate.cfg.xml主配置文件
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="foo">
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:HR</property>
<property name="connection.username">happy</property>
<property name="connection.password">happy</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="cn/happy/daymanymany/Project.hbm.xml"/>
<mapping resource="cn/happy/daymanymany/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
================================================================
6.书写测试类和方法
package cn.happy.test;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import cn.happy.daymanymany.Employee;
import cn.happy.daymanymany.Project;
public class Day06OneManyTest {
// 创建session工厂
public static SessionFactory factory;
// 创建Session对象
public static Session session;
// 通过代码块赋值
static {
factory = new Configuration().configure().buildSessionFactory();
session = factory.openSession();
}
@Test
public void addTest(){
Transaction tran = null;
try {
tran = session.beginTransaction();
//创建EMP对象
Employee emp=new Employee();
emp.setEmpname("李小龙");
//创建Pro对象
Project pro=new Project();
pro.setProname("海淀花园");
//指定工程需要的员工
pro.getEmployees().add(emp);
//指定员工所属的工程
emp.getProjects().add(pro);
session.save(pro);
// 事务提交
tran.commit();
System.out.println("成功");
} catch (Exception e) {
e.printStackTrace();
if (tran != null) {
tran.rollback();
}
throw new RuntimeException("错误");
} finally {
// 关闭session
session.close();
}
}
}
=========================================================================