程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> Hibernate關聯映射 映射文件的配置,

Hibernate關聯映射 映射文件的配置,

編輯: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.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="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 class="Employee"/>
           </set>
    </class>
</hibernate-mapping>

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="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 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.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="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 class="Project" column="rproNo"></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.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 class="Employee" 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();
    
}

就到這裡了,有問題可以在線解答!!!!!!!!!!!!

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved