- 浏览: 1235456 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (193)
- ant/maven (6)
- algorithm (5)
- tomcat/weblogic/jboss (6)
- javascript/jquery (13)
- java (33)
- flex/flash (0)
- JPA/Hibernate/myBatis (18)
- java concurrent (7)
- test (2)
- windows/linux (6)
- java collection (7)
- design pattern (2)
- life/health (3)
- database (12)
- IDE (4)
- spring/ejb (20)
- html/css/ckeditor (7)
- jsp/servlet (3)
- java io (13)
- java security (4)
- jni (0)
- svn/git (2)
- english (2)
- java jmx (1)
- xml (1)
- struts/springmvc (9)
- middleware (2)
- cache (1)
- cglib (3)
最新评论
-
jlotusYo:
博主,真感谢。
Java 密码扩展无限制权限策略文件 -
senninha:
这个。。是api说明吧。。
ScheduledExecutorService 源码分析 -
zoutao2008:
请问大文件如何处理?按你这种方式的话,文件超过200M时就会报 ...
hessian系列之二:上传文件 -
lwj1113:
lwj1113 写道谢谢博主这么细致的demo;在系列五中通过 ...
myBatis系列之五:与Spring3集成 -
lwj1113:
谢谢博主这么细致的demo;在系列五中通过testng测试类跑 ...
myBatis系列之五:与Spring3集成
实体是Employee和Department,它们之间是多对一的关系。
Department类:
Employee类:
Department.hbm.xml:
Employee.hbm.xml:
many-to-one没有inverse属性,因为关系的维护是many的一方,不可能放弃对关系的维护。
many-to-one的lazy属性有三个取值:false, proxy, no-proxy。
1. 测试cascade属性:
结果是报org.hibernate.TransientObjectException异常,因为没有保存Department实例。
可以加cascade属性,解决问题:
2. 测试fetch
查询语句如下:
Hibernate: select employee0_.id as id1_0_, employee0_.name as name1_0_, employee0_.department as department1_0_, employee0_.skill as skill1_0_, employee0_.sell as sell1_0_, employee0_.type as type1_0_ from Employee employee0_ where employee0_.id=?
Hibernate: select department0_.id as id0_0_, department0_.name as name0_0_ from Department department0_ where department0_.id=?
因为fetch设置为select,所以对每个实体,都分别用一个SELECT语句
如果把fetch设置为join,也就是连表查询,只使用一个SELECT语句。如下:
Hibernate: select employee0_.id as id1_1_, employee0_.name as name1_1_, employee0_.department as department1_1_, employee0_.skill as skill1_1_, employee0_.sell as sell1_1_, employee0_.type as type1_1_, department1_.id as id0_0_, department1_.name as name0_0_ from Employee employee0_ left outer join Department department1_ on employee0_.department=department1_.id where employee0_.id=?
3. 测试lazy
当fetch为select时,设置lazy为proxy或者no-proxy。
结果是报org.hibernate.LazyInitializationException异常。
因为fetch为select,而且lazy为proxy或者no-proxy,所以开始仅仅查询Employee,当需要用SELECT语句查询Department时,Session已经关闭。
解决办法:
1. 设置lazy为false,hibernate会第一时间把Employee和Department查询出来。
如果fetch为select,使用两个SELECT查询语句。
如果fetch为join,使用一个SELECT连表查询语句。
2. 设置fetch为join,这时不管lazy的取值,hibernate会进行连表查询,把两个实体都查询出来。
Department类:
public class Department { private int id; private String name; public Department() { } public Department(String name) { this.name = name; } // getters and setters are omitted }
Employee类:
public class Employee { private int id; private String name; private Department department; public Employee() { } public Employee(String name) { this.name = name; } // getters and setters are omitted
Department.hbm.xml:
<hibernate-mapping package="com.john.myhibernate.domain"> <class name="Department"> <id name="id"> <generator class="native"/> </id> <property name="name" length="20" not-null="true"/> </class> </hibernate-mapping>
Employee.hbm.xml:
<hibernate-mapping package="com.john.myhibernate.domain"> <class name="Employee"> <id name="id"> <generator class="native"/> </id> <property name="name" length="20" not-null="true"/> <many-to-one name="department" column="department_id" class="Department" fetch="select"/> </class> </hibernate-mapping>
many-to-one没有inverse属性,因为关系的维护是many的一方,不可能放弃对关系的维护。
many-to-one的lazy属性有三个取值:false, proxy, no-proxy。
1. 测试cascade属性:
public void testSaveCascade() { Session s = null; Transaction tx = null; Department depart = new Department(); depart.setName("FCI"); Employee em1 = new Employee("John"); em1.setDepartment(depart); Employee em2 = new Employee("Lucy"); em2.setDepartment(depart); try { s = HibernateUtil.getSession(); tx = s.beginTransaction(); s.save(em1); s.save(em2); tx.commit(); } catch (HibernateException e) { tx.rollback(); e.printStackTrace(); } finally { if (s != null) s.close(); } }
结果是报org.hibernate.TransientObjectException异常,因为没有保存Department实例。
可以加cascade属性,解决问题:
<many-to-one name="department" column="department_id" class="Department" fetch="select" cascade="save-update"/>
2. 测试fetch
Session s = null; s = HibernateUtil.getSession(); Employee em = (Employee) s.get(Employee.class, 2); System.out.println(em.getName()); System.out.println(em.getDepartment());
查询语句如下:
Hibernate: select employee0_.id as id1_0_, employee0_.name as name1_0_, employee0_.department as department1_0_, employee0_.skill as skill1_0_, employee0_.sell as sell1_0_, employee0_.type as type1_0_ from Employee employee0_ where employee0_.id=?
Hibernate: select department0_.id as id0_0_, department0_.name as name0_0_ from Department department0_ where department0_.id=?
因为fetch设置为select,所以对每个实体,都分别用一个SELECT语句
如果把fetch设置为join,也就是连表查询,只使用一个SELECT语句。如下:
Hibernate: select employee0_.id as id1_1_, employee0_.name as name1_1_, employee0_.department as department1_1_, employee0_.skill as skill1_1_, employee0_.sell as sell1_1_, employee0_.type as type1_1_, department1_.id as id0_0_, department1_.name as name0_0_ from Employee employee0_ left outer join Department department1_ on employee0_.department=department1_.id where employee0_.id=?
3. 测试lazy
当fetch为select时,设置lazy为proxy或者no-proxy。
<many-to-one name="department" column="department_id" class="Department" fetch="select" cascade="save-update" lazy="no-proxy"/>
Session s = null; s = HibernateUtil.getSession(); Employee em = (Employee) s.get(Employee.class, 2); s.close(); System.out.println(em.getName()); System.out.println(em.getDepartment());
结果是报org.hibernate.LazyInitializationException异常。
因为fetch为select,而且lazy为proxy或者no-proxy,所以开始仅仅查询Employee,当需要用SELECT语句查询Department时,Session已经关闭。
解决办法:
1. 设置lazy为false,hibernate会第一时间把Employee和Department查询出来。
如果fetch为select,使用两个SELECT查询语句。
如果fetch为join,使用一个SELECT连表查询语句。
2. 设置fetch为join,这时不管lazy的取值,hibernate会进行连表查询,把两个实体都查询出来。
发表评论
-
myBatis系列之七:事务管理
2014-04-01 15:36 103926myBatis系列之一:搭建开发环境 myBatis系列之二: ... -
myBatis系列之八:分页查询
2014-01-13 22:07 0分页查询:物理分页,不是内存分页: 1. 在sql语句中指定 ... -
myBatis系列之六:与SpringMVC集成
2013-12-26 13:33 5887myBatis系列之一:搭建开发环境 myBatis系列之二: ... -
myBatis系列之五:与Spring3集成
2013-12-09 14:31 4979myBatis系列之一:搭建开 ... -
myBatis系列之四:关联数据的查询
2013-12-08 13:36 9704myBatis系列之一:搭建开发环境 myBatis系列之二: ... -
myBatis系列之三:增删改查
2013-12-01 22:36 5028myBatis系列之一:搭建开发环境 myBatis系列之二: ... -
myBatis系列之二:以接口方式交互数据
2013-11-25 22:46 12355myBatis系列之一:搭建开发环境 myBatis系列之三: ... -
myBatis系列之一:搭建开发环境
2013-11-18 22:00 8537myBatis系列之二:以接口方式交互数据 myBatis系列 ... -
JPA 一对一关联
2012-08-18 17:12 3231在一对一关联中,JPA没有规定谁为关系的维护方,OneToOn ... -
Hibernate 集合类型使用
2012-08-11 18:31 1330为了映射实体中的集合 ... -
Hibernate 组件关联关系
2012-08-11 15:10 1241实体关联的属性是个复杂类型的持久化类,但不是实体:数据库中没有 ... -
Hibernate 一对多关联配置
2012-08-09 22:13 1839以Department和Employee为例。 Depart ... -
Hibernate 继承关系配置
2012-08-08 23:33 4046以Department和Employee实体为例。Develo ... -
Hibernate 查询分析
2012-08-08 19:15 0private void fireLoad(LoadEv ... -
Hibernate 配置文件
2012-07-25 20:17 0<properties> < ... -
jdbc 批处理操作
2012-05-15 15:43 1405jdbc的批处理操作是指把一组SQL语句(增删改操作)一次性提 ... -
jdbc调用postgreSQL数据库内置函数
2012-05-08 16:02 31141. current_timestamp 返回的是当前的 ... -
构建Hibernate项目
2012-04-25 20:35 01. 新建一个Java Project 2. 添加hiber ... -
乐观锁与悲观锁
2012-04-24 22:50 2755悲观锁: 读取数据的时候对记录加锁,直到提交数据更新后解锁 ... -
Hibernate映射文件的配置
2012-04-20 22:21 01. type值不是指java类型或者数据库的类型,而是Hib ...
相关推荐
Hibernate双向一对一关联映射(注解版)
Hibernate多表关联配置及错误解决方法,及时更新,相关信息待下回分解!
NULL 博文链接:https://profound-accumulation.iteye.com/blog/2243079
Hibernate_关联关系映射配置
NULL 博文链接:https://yinxiaoyong.iteye.com/blog/425782
hibernate基于主外键的一对多/多对一关联
针对hibernate中关于对象间的关联关系的配置
NULL 博文链接:https://dreamzhong.iteye.com/blog/1200915
在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟话说,萝卜青菜,可有所爱,每个人都有自己喜欢的配置方式,我在试了这两种方式以后,发现使用...
在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟话说,萝卜青菜,可有所爱,每个人都有自己喜欢的配置方式,这个是xml配置的例子
Hibernate关联关系配置,一对一,一对多,多对多,联合主键等的配置
Hibernate_关联关系映射配置详解,希望能帮助广大java爱好者
多对一的配置方式有两种:(1)通过@JoinColoumn映像(2)通过关联表的方式来映像 (1) 通过@JoinColoumn映射 SRD Framework中Company,Category例子: Company: @ManyToOne @JoinColumn(name = "CATEGORY_OPTION_ID")...
介绍使用Hibernate注解来进行关联关系的配置,结合我的博客文章阅读更方便理解。
详细javabean配置 博文链接:https://871421448.iteye.com/blog/1561798
关于hibernate3.0各种数据库关联的小列子,里面的内容分的很仔细很有条理。对于初学者很有帮助。
直至Hibernate实战,重点讲述了Hibernate的基础语法、基础配置、O/R映射、数据关联、数据检索、HQL实用技术、自定义持久化实现、Hibernate回调与拦截、Hibernate分页等实用技术,Hibernate实战部分则用一个真实论坛...
一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在...
hibernate表之间的关联关系在配置文件如何体现
案例目录: 01 01Hibernate_Handwork : 手工配置使用Hibernate,其中详细标了Hibernate进行持久化的一些过程,因为是Hibernate的入门实例,所以注释很详细,其中有... 一对一、多对一、一对多、多对多等几种情况。