你的位置:首页 > Java教程

[Java教程]Hibernate关联映射的 多对多的两种配置


我们谈到关联映射的多对多配置呢,有一种挺不好配置的,所以呢,就放到了这里给大家参考

第一种:建立多对多双向关联 拆成两个一对多

三个实体类

Employee

 private Integer empno;   private String ename;     private Dept dept;   private Set<Project> projects=new HashSet<Project>(0);//集合 类 employee的属性   private Set<ProEmp> pe=new HashSet<ProEmp>(0);

Project

private Integer proNo;//项目的编号private String proName;//项目名称private Set<Employee> employees=new HashSet<Employee>(0);//集合 类 employee的属性private Set<ProEmp> pe=new HashSet<ProEmp>(0);

中间表

ProEmp

  private Integer id;  private Project pro;  private Employee emp;

映射文件

Employee.hbm.

<?"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.three.entity">  <class name="Employee" table="Employee">    <id name="empno" column="empno">      <generator class="native"/>    </id>    <property name="ename" type="string" column="ename"/>  </class></hibernate-mapping>

Project.hbm.

<?"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.three.entity">  <class name="Project" table="Project">    <id name="proNo" column="proNo">      <generator class="native"/>    </id>    <property name="proName" type="string" column="proName"/>  </class></hibernate-mapping>

ProEmp.hbm.

<?"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.three.entity">  <class name="ProEmp" table="ProEmp">    <id name="id" column="id">      <generator class="native"/>    </id>     <!-- 与员工关联 -->    <many-to-one name="emp" column="empno" ></many-to-one>      <!-- -与project关联 -->   <many-to-one name="pro" column="proNo"></many-to-one>  </class></hibernate-mapping>

大配置文件hibernate.cfg.

<?'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>    <!-- Database connection settings -->    <property name="connection.driver_class">      oracle.jdbc.driver.OracleDriver    </property>    <property name="connection.url">      jdbc:oracle:thin:@localhost:1521:orcl    </property>    <property name="connection.username">wgy</property>    <property name="connection.password">jpp</property>    <!-- JDBC connection pool (use the built-in) -->    <property name="connection.pool_size">1</property>    <!-- SQL dialect --><!-- SQL 方言 -->    <property name="dialect">      org.hibernate.dialect.Oracle10gDialect    </property>    <!-- Enable Hibernate's automatic session context management -->    <!-- <property name="current_session_context_class">thread</property>    -->    <!-- Disable the second-level cache -->    <!-- <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>    -->    <!-- Echo all executed SQL to stdout -->    <property name="show_sql">true</property>    <!-- Drop and re-create the database schema on startup -->    <!--序列化 -->    <property name="hbm2ddl.auto">create</property>    <!-- <property name="hbm2ddl.auto">update</property> -->    <!-- sql语句格式化 -->    <property name="format_sql">true</property>    <!-- 资源 -->
<<mapping resource="cn/three/entity/Dept.hbm. </session-factory></hibernate-configuration>

测试类看一下效果

package cn.main;import org.hibernate.Session;import cn.three.entity.ProEmp;import cn.three.entity.Employee;import cn.three.entity.Project;import cn.util.HibernateUtil;public class ThreeTest {public static void main(String[] args) {  //三表一起    threeAddEmpAndProAndProEmp();}private static void threeAddEmpAndProAndProEmp() {  Session session = HibernateUtil.currentSession();  session.beginTransaction();  Employee emp=new Employee();  Project pro=new Project();  emp.setEname("李四");  pro.setProName("一号项目");  ProEmp pe=new ProEmp();  pe.setEmp(emp);  pe.setPro(pro);  session.save(pro);  session.save(emp);  session.save(pe);  session.getTransaction().commit();  HibernateUtil.closeSessio();  System.out.println("add ok");  }}

第二种:不用拆成两个一对多

只需要两个映射文件,即可

Employee.hbm.

<?"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.entity">  <class name="Employee" table="Employee">    <id name="empno" column="empno">      <generator class="native"/>    </id>    <property name="ename" type="string" column="ename"/>    <!--多对一 -->    <many-to-one name="dept" column="deptNo" ></many-to-one>    <!--多对多双向关联 -->    <set name="projects" inverse="true" table="proemp">    <key column="rempNo"></key>    <many-to-many column="rproNo"></many-to-many>    </set>  </class></hibernate-mapping>

Project.hbm.

<?"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.entity">  <class name="Project" table="Project">    <id name="proNo" column="proNo">      <generator class="native"/>    </id>    <property name="proName" type="string" column="proName"/>   <!--配置单向多对多关联 -->    <!-- 映射project 类的employees的属性 -->    <!--cascade="save-update":表名保存或更新project对象时,会级联保存或更新与它关联的Employee对象 -->    <set name="employees" table="proemp" cascade="save-update">     <key column="rproNo"></key>     <many-to-many column="rempNo"></many-to-many>    </set>  </class></hibernate-mapping>

测试类

private static void addProToEmpAndEmpToPro() {  Session session = HibernateUtil.currentSession();  session.beginTransaction();  Employee emp=new Employee();    Project pro=new Project();    emp.setEname("李四");    pro.setProName("一号项目");  pro.getEmployees().add(emp);  session.save(pro);  session.getTransaction().commit();  HibernateUtil.closeSessio();  System.out.println("add ok");

这样就可以了,有问题在线解答!!!!!11111111