歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Hibernate關聯映射---多對一實例分析(單項關聯)

一  關聯映射類型

1.多對一(Employee - Department)

    多對一關聯是一個部門可以有多個員工

    所用到的映射文件為:

       <many-to-one name="depart" column="depart_id"/>

二  代碼分析

1.首先創建兩個類,Department和Employee,並把他們關聯起來

(1)Department類

package com.hbsi.domain;

//部門類

public class Department {

    private int id;

    private String name;

    public Department() {

       super();

       // TODO Auto-generated constructor stub

    }

    public Department(int id, String name) {

       super();

       this.id = id;

       this.name = name;

    }

    public int getId() {

       return id;

    }

    public void setId(int id) {

       this.id = id;

    }

    public String getName() {

       return name;

    }

    public void setName(String name) {

       this.name = name;

    }

    public String toString() {

       return "Department [id=" + id + ", name=" + name + "]";

    }

}

(2)Employee類

    package com.hbsi.domain;                                                                                                                                                       

//員工類    一般主鍵是建在多的一方                                                                 

public class Employee {                                                                

    private int id;                                                                    

    private String name;                                                                                                                     

    // 得到的是一個 對象,可以得到員工對應的部門的詳細信息                                                      

    private Department depart;                                                         

                                                                                       

    public Employee() {                                                                

       super();                                                                       

       // TODO Auto-generated constructor stub                                        

    }                                                                                   

                                                                                       

    public Employee(int id, String name, Department depart) {                           

       super();                                                                       

       this.id = id;                                                                  

       this.name = name;                                                              

       this.depart = depart;                                                          

    }                                                                                  

                                                                                       

    public int getId() {                                                               

       return id;                                                                     

    }                                                                                  

                                                                                        

    public void setId(int id) {                                                        

       this.id = id;                                                                  

    }                                                                                  

                                                                                       

    public String getName() {                                                           

       return name;                                                                   

    }                                                                                  

                                                                                        

    public void setName(String name) {                                                 

       this.name = name;                                                              

    }                                                                                   

                                                                                       

    public Department getDepart() {                                                    

       return depart;                                                                  

    }                                                                                  

                                                                                       

    public void setDepart(Department depart) {                                          

       this.depart = depart;                                                          

    }                                                                                                                                                                                                                                            

    public String toString() {                                                         

       return "Employee [id=" + id + ", name=" + name + ", depart=" + depart          

              + "]";                                                                 

    }                                                                                  

}         

注:之所以在員工表中設主鍵是為了減少資源消耗,而如果設置

private int depart_id;     

通過員工查找部門的話,只能找到部門的id,得不到部門的其他信息 ,                                      

設置的Deparment的類對象作為外鍵,可以得到部門的所有信息

                                                                            

Copyright © Linux教程網 All Rights Reserved