Hibernate关联映射之_多对一
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Hibernate关联映射之_多对一,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4763字,纯文字阅读大概需要7分钟。
内容图文
多对一 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 } 1819publicint getId() { 20return id; 21 } 2223publicvoid setId(int id) { 24this.id = id; 25 } 2627public String getName() { 28return name; 29 } 3031publicvoid setName(String name) { 32this.name = name; 33 } 3435 }
员工:
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 } 2122publicint getId() { 23return id; 24 } 2526publicvoid setId(int id) { 27this.id = id; 28 } 2930public String getName() { 31return name; 32 } 3334publicvoid setName(String name) { 35this.name = name; 36 } 3738public Department getDepartment() { 39return department; 40 } 4142publicvoid setDepartment(Department department) { 43this.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; 9try { 10 session = HibernateUtils.getSession(); 11 transaction = session.beginTransaction(); 1213 employee.setDepartment(department); 14 session.save(department); 15 session.save(employee); 1617 transaction.commit(); 1819 }finally { 20if (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; 4try { 5 session = HibernateUtils.getSession(); 6 transaction = session.beginTransaction(); 7 8/*通过department_id查询department,将部门与员工关联起来*/ 9 employee.setDepartment(departmentDao.getDepartment(employee.getDepartment())); 10 session.save(employee); 1112 transaction.commit(); 13 }finally { 14if (session != null){ 15 session.close(); 16 } 17 } 18 }
上段代码,员工想要关联的部门信息已经在数据库中了,不需要重新插入,那么怎么关联呢?答案就是在插入员工信息前,先通过部门的查询条件从数据库中查询到部门信息,将部门信息设置给员工,然后再保存员工。此时数据库中的员工记录也和部门信息有了关联。
1 public Employee getEmployee(Employee employee){ 2 Session session = null; 3 Employee employee1 = null; 4try { 5 session = HibernateUtils.getSession(); 6 employee1 = (Employee) session.get(Employee.class,employee.getId()); 7 System.out.println(employee1);//不知道为什么,不先使用对象的话,session关闭后在方法体外就无法使用了。这里的懒加载干嘛了? 8 }finally { 9if (session != null){ 10 session.close(); 11 } 12 } 13return employee1; 1415 }
上段代码,查询条件就只有员工的id,但是通过映射文件,其对应的部门信息也会被填充到员工信息中
原文:http://www.cnblogs.com/sherrykid/p/4587110.html
内容总结
以上是互联网集市为您收集整理的Hibernate关联映射之_多对一全部内容,希望文章能够帮你解决Hibernate关联映射之_多对一所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。