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的类对象作为外键,可以得到部门的所有信息

                                                                            

  • 1
  • 2
  • 3
  • 下一页

相关内容