你的位置:首页 > Java教程

[Java教程]Hibernate关联映射 映射文件的配置


一:多对一单向关联

首先我们必须创建两个实体类

例如:Dept类

public class Dept {private Integer deptNo;private String dName;private String loc;private Set<Employee> emps=new HashSet<Employee>();

Employee类

public class Employee {   private Integer empno;   private String ename;     private Dept dept;

 

现在我们可以在配置文件里面配置了

Dept.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="Dept" table="Dept">    <id name="deptNo" column="deptNo">      <generator class="native"/>    </id>    <property name="dName" type="string" column="dName"/>      <property name="loc" type="string" column="loc"/>      <!--cascade:级联   inverse:反转 -->      <set name="emps" cascade="save-update" inverse="true" >      <key column="deptNo"></key>      <!--一对多 -->      <one-to-many />      </set>  </class></hibernate-mapping>

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 class="Project" column="rproNo"></many-to-many>    </set>  </class></hibernate-mapping>

到这里,一对多 和多对一的关联 配置已经完成!!

我们一起来看看测试类

// 添加数据     private static void addDeptAndEmployee() {    Session currentSession = HibernateUtil.currentSession();    currentSession.beginTransaction();    Dept dept = new Dept();    //dept.setDeptNo(deptNo)    dept.setdName("质管部22");    dept.setLoc("北京22");    Employee emp = new Employee();    emp.setEname("龙龙");      emp.setDept(dept);//建立dept 到employee 对象的 关联    currentSession.save(emp);    currentSession.save(dept);    currentSession.getTransaction().commit();    HibernateUtil.closeSessio();    System.out.println("add ok");  }

 

// 级联保存  cascade:级联      private static void addDeptAndEmployeeTocascade() {    Session currentSession = HibernateUtil.currentSession();    currentSession.beginTransaction();    Dept dept = new Dept();    // dept.setDeptNo(deptNo)    dept.setdName("质管部22");    dept.setLoc("北京22");    Employee emp = new Employee();    emp.setEname("龙龙");    // 建立Dept对象 和Employee 对象的 一对多 双向关系    emp.setDept(dept);    dept.getEmps().add(emp);//    currentSession.save(dept);    currentSession.getTransaction().commit();    HibernateUtil.closeSessio();    System.out.println("add ok");  }

 

现在我们开始来看看多对多的关联

我们还是创建好实体类

例如:

Employee

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

Project

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

 

注意:我还在了Oracle 工具里面创建了 另外一个表proemp ,即 连接表,你看一下外键关系就可以知道了

create table proemp(rproNo number(6),rempNo number(6))alter table proemp add constraints FK_rproNo FOREIGN KEY(rproNo) REFERENCES Project(prono)alter table proemp add constraints FK_rempNo FOREIGN KEY(rempNo) REFERENCES employee(empNo)

 

然后来配置映射文件

单向  多对多关联

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" class="Dept"></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>

双向 多对多

<?"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();  Employee emp2=new Employee();  Project pro=new Project();  Project pro2=new Project();  emp.setEname("李四");  emp2.setEname("张三");  pro.setProName("一号项目");  pro2.setProName("二号项目");  pro.getEmployees().add(emp);  pro.getEmployees().add(emp2);    emp.getProjects().add(pro);  emp.getProjects().add(pro2);    pro2.getEmployees().add(emp);    emp.getProjects().add(pro2);    session.save(pro);  session.save(pro2);  HibernateUtil.closeSessio();  }

 

//保存Project 对象的同时还能保存 Employee对象private static void addProToEmp() {  Session session = HibernateUtil.currentSession();  session.beginTransaction();  Employee emp=new Employee();  Employee emp2=new Employee();  Project pro=new Project();  Project pro2=new Project();  emp.setEname("李四");  emp2.setEname("张三");  pro.setProName("一号项目");  pro2.setProName("二号项目");   pro.getEmployees().add(emp);  pro.getEmployees().add(emp2);  pro2.getEmployees().add(emp);  session.save(pro);  session.save(pro2);  HibernateUtil.closeSessio();  }

就到这里了,有问题可以在线解答!!!!!!!!!!!!