码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate关联映射之_一对多

时间:2015-06-20 00:12:50      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

domain:

 1 package org.zln.hibernate.domain;
 2 
 3 import java.util.Set;
 4 
 5 /**
 6  * 部门Domain对象
 7  * Created by sherry on 000018/6/18 21:38.
 8  */
 9 public class Department {
10     private int id;
11     private String name;
12 
13     private Set<Employee> employeeSet;
14 
15     @Override
16     public String toString() {
17         return "Department{" +
18                 "id=" + id +
19                 ", name=‘" + name + ‘\‘‘ +
20                 ‘}‘;
21     }
22 
23     public Set<Employee> getEmployeeSet() {
24         return employeeSet;
25     }
26 
27     public void setEmployeeSet(Set<Employee> employeeSet) {
28         this.employeeSet = employeeSet;
29     }
30 
31     public int getId() {
32         return id;
33     }
34 
35     public void setId(int id) {
36         this.id = id;
37     }
38 
39     public String getName() {
40         return name;
41     }
42 
43     public void setName(String name) {
44         this.name = name;
45     }
46 
47 }

映射文件

 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         <set name="employeeSet">
14             <!--主键作为条件查询员工表的外键-->
15             <key column="department_id"/>
16             <!--集合中存储元素的类型-->
17             <one-to-many class="Employee" />
18         </set>
19 
20     </class>
21 
22 </hibernate-mapping>

Dao

 1     public Department getDepartment(Department department){
 2         Department target = null;
 3         Session session = null;
 4         try {
 5             session = HibernateUtils.getSession();
 6             target = (Department) session.get(Department.class,department.getId());
 7             Hibernate.initialize(target.getEmployeeSet());
 8         }finally {
 9             if (session != null){
10                 session.close();
11             }
12         }
13         return target;
14     }

在查询部门的时候,根据配置的一对多的关系,会将部门对应的员工信息也查询出来映射到Set中

 1     /**
 2      * 新增部门及其员工
 3      * @param department
 4      * @param employeeSet
 5      */
 6     public void addDepartment(Department department,Set<Employee> employeeSet){
 7         Session session = null;
 8         Transaction transaction = null;
 9         try {
10             session = HibernateUtils.getSession();
11             transaction = session.beginTransaction();
12 
13             department.setEmployeeSet(employeeSet);
14 
15             /*保存部门*/
16             session.save(department);
17             /*保存员工*/
18             for (Employee employee:employeeSet){
19                 session.save(employee);
20             }
21             transaction.commit();
22         }finally {
23             if (session != null){
24                 session.close();
25             }
26         }
27     }

保存的员工会自带其部门外键

Hibernate关联映射之_一对多

标签:

原文地址:http://www.cnblogs.com/sherrykid/p/4589889.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!