Hibernate关联映射之_多对一

多对一  Employee-Department

对于 员工 和 部门 两个对象,从员工的角度来看,就是多对一的一个关系--->多个员工对应一个部门

 

表设计:

  部门表:department,id主键

  员工表:employee,id主键,depart_id作为外键,与部门表的主键对应

 

对象模型设计:

  部门:

 1 package org.zln.hibernate.domain;
 2 
 3 /**
 4  * 部门Domain对象
 5  * Created by sherry on 000018/6/18 21:38.
 6  */
 7 public class Department {
 8     private int id;
 9     private String name;
10 
11     @Override
12     public String toString() {
13         return "Department{" +
14                 "id=" + id +
15                 ", name='" + name + '\'' +
16                 '}';
17     }
18 
19     public int getId() {
20         return id;
21     }
22 
23     public void setId(int id) {
24         this.id = id;
25     }
26 
27     public String getName() {
28         return name;
29     }
30 
31     public void setName(String name) {
32         this.name = name;
33     }
34 
35 }

 

  员工:

 1 package org.zln.hibernate.domain;
 2 
 3 /**
 4  * 员工Domain对象
 5  * Created by sherry on 000018/6/18 21:39.
 6  */
 7 public class Employee {
 8     private int id;
 9     private String name;
10     /*employee隶属于一个department*/
11     private Department department;
12 
13     @Override
14     public String toString() {
15         return "Employee{" +
16                 "id=" + id +
17                 ", name='" + name + '\'' +
18                 ", department=" + department +
19                 '}';
20     }
21 
22     public int getId() {
23         return id;
24     }
25 
26     public void setId(int id) {
27         this.id = id;
28     }
29 
30     public String getName() {
31         return name;
32     }
33 
34     public void setName(String name) {
35         this.name = name;
36     }
37 
38     public Department getDepartment() {
39         return department;
40     }
41 
42     public void setDepartment(Department department) {
43         this.department = department;
44     }
45 }

XML映射文件配置

  员工:

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping package="org.zln.hibernate.domain">
 6 
 7     <class name="Employee" table="employee">
 8         <!--单字段主键-->
 9         <id name="id" column="id">
10             <generator class="native"/>
11         </id>
12         <!--普通字段-->
13         <property name="name" column="name"/>
14         <!--多对一字段--><!--默认查找员工表中的department_id==部门表中的主键id的部门信息,也可以手动指定 property-ref-->
15         <many-to-one name="department" column="department_id"/>
16     </class>
17 
18 </hibernate-mapping>

  部门:

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping package="org.zln.hibernate.domain">
 6 
 7     <class name="Department" table="department">
 8         <id name="id" column="id">
 9             <generator class="native"/>
10         </id>
11         <property name="name" column="name"/>
12 
13     </class>
14 
15 </hibernate-mapping>

说明:部门的映射文件没有什么不同,但是在员工的映射文件中,映射domain对象中的department对象的,是department_id字段,Hibernate会查询employee表中的department_id,以此为条件,

  再去查询department表中的记录,department_id对应department表中的id(默认情况外键对应主键嘛)。然后将从department表中的记录填充到employee对象中的department成员变量。

Dao:

 1     /**
 2      * 添加员工及其部门
 3      * @param department
 4      * @param employee
 5      */
 6     public void addEmployee(Department department,Employee employee){
 7         Session session = null;
 8         Transaction transaction = null;
 9         try {
10             session = HibernateUtils.getSession();
11             transaction = session.beginTransaction();
12 
13             employee.setDepartment(department);
14             session.save(department);
15             session.save(employee);
16 
17             transaction.commit();
18 
19         }finally {
20             if (session != null){
21                 session.close();
22             }
23         }
24     }

上段代码,往数据库的员工表与部门表同时插入了一条记录,通过  employee.setDepartment(department);    将部门记录管理到了员工记录中

先插部门,会生成一个id作为员工表的外键,在插员工,此时员工信息+外键,都会插入到员工表中。如果先插入员工,那么此时员工表的外键是空的,当插入部门后,部门主键生成,员工的外键才有值,此时Hibernate在提交前还会执行一条update语句。从结果上看是一样的,但是多执行了一条。所以最好还是先执行部门的插入操作。

 1     public void addEmployee(Employee employee){
 2         Session session = null;
 3         Transaction transaction = null;
 4         try {
 5             session = HibernateUtils.getSession();
 6             transaction = session.beginTransaction();
 7 
 8             /*通过department_id查询department,将部门与员工关联起来*/
 9             employee.setDepartment(departmentDao.getDepartment(employee.getDepartment()));
10             session.save(employee);
11 
12             transaction.commit();
13         }finally {
14             if (session != null){
15                 session.close();
16             }
17         }
18     }

上段代码,员工想要关联的部门信息已经在数据库中了,不需要重新插入,那么怎么关联呢?答案就是在插入员工信息前,先通过部门的查询条件从数据库中查询到部门信息,将部门信息设置给员工,然后再保存员工。此时数据库中的员工记录也和部门信息有了关联。

 1     public Employee getEmployee(Employee employee){
 2         Session session = null;
 3         Employee employee1 = null;
 4         try {
 5             session = HibernateUtils.getSession();
 6             employee1 = (Employee) session.get(Employee.class,employee.getId());
 7             System.out.println(employee1);//不知道为什么,不先使用对象的话,session关闭后在方法体外就无法使用了。这里的懒加载干嘛了?
 8         }finally {
 9             if (session != null){
10                 session.close();
11             }
12         }
13         return employee1;
14 
15     }

上段代码,查询条件就只有员工的id,但是通过映射文件,其对应的部门信息也会被填充到员工信息中

 

posted @ 2015-06-18 21:36  csnmd  阅读(184)  评论(0编辑  收藏  举报