`

Hibernate性能问题:延迟加载

阅读更多
HibernateDaoSupper对hibernate的支持
用getHibernateTemplate(),不支持延迟加载,因为它的session一用完就关闭;
用getSession()可以支持延迟加载,而session的关闭也是Spring的事物帮你关闭它.不需要你动手。
例子与区别:
public Instructor findInstructorById(Integer id) {
         return (Instructor) getHibernateTemplate().get(Instructor.class, id);
}


public Instructor findInstructorById(Integer id) {
		return (Instructor)getSession().get(Instructor.class, id);
		
	}


BeanFactory ac = new FileSystemXmlApplicationContext(
			"WebRoot/WEB-INF/applicationContext.xml");
InstructorDao instructorDao=(InstructorDao)ac.getBean("instructorDao");
Instructor instructor=instructorDao.findInstructorById(1);
System.out.println(instructor.getName());
System.out.println(instructor.getCourses().size());


getHibernateTemplate()的结果(出异常了):
Hibernate: select instructor0_.id as id0_0_, instructor0_.name as name0_0_, instructor0_.password as password0_0_ from instructor instructor0_ where instructor0_.id=?
tiger
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.huanglq.model.Instructor.courses, no session or session was closed
	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
	at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:97)
	at org.hibernate.collection.PersistentSet.size(PersistentSet.java:114)
	at com.huanglq.test.Test.main(Test.java:41)


getSession()的结果:
Hibernate: select instructor0_.id as id0_0_, instructor0_.name as name0_0_, instructor0_.password as password0_0_ from instructor instructor0_ where instructor0_.id=?
tiger
Hibernate: select courses0_.id as id1_, courses0_.id as id3_0_, courses0_.instructor_id as instructor2_3_0_, courses0_.name as name3_0_, courses0_.state as state3_0_, courses0_.startDate as startDate3_0_, courses0_.endDate as endDate3_0_, courses0_.description as descript7_3_0_, courses0_.maxstudentcount as maxstude8_3_0_ from course courses0_ where courses0_.id=?
Hibernate: select students0_.course_id as course2_1_, students0_.stu_id as stu1_1_, student1_.id as id1_0_, student1_.name as name1_0_, student1_.password as password1_0_, student1_.age as age1_0_, student1_.address as address1_0_, student1_.state as state1_0_, student1_.phone as phone1_0_, student1_.email as email1_0_, student1_.zip as zip1_0_, student1_.description as descrip10_1_0_ from stu_cour students0_ left outer join student student1_ on students0_.stu_id=student1_.id where students0_.course_id=?
1
分享到:
评论

相关推荐

    Hibernate性能(缓存 延迟加载 事务 悲观 乐观锁).ppt

    性能------------缓存 延迟加载 事务 悲观 乐观锁

    Hibernate 延迟加载剖析与代理模式应用

    Hibernae 的延迟加载是一个非常常用的技术,实体的集合属性默认会被延迟加载,实体所关联的实体默认也会被延迟加载。Hibernate 通过这种延迟加载来降低系统的内存开销,从而保证 Hibernate 的运行性能。

    属性延迟加载

    Hibernate3开始增加了通过property节点的lazy属性,为特定的属性指定延迟加载策略,以避免实体整体加载可能带来的性能浪费,尤其是像长文本之类的大字段。那么实现属性延迟加载需要做两件事: 1.修改映射配置...

    Hibernate延迟加载

    hibernate延迟加载机制是为了避免一些无谓的性能开销而提出来的,所谓延迟加载就是当在真正需要数据的时候,才真正执行数据加载操作!

    hibernate 延迟加载.docx

    Hibernate在查询某个对象时,立即查询与之关联的对象: 1、当select的语句数目太多,需要频繁的访问数据库,会影响查询的性能。 2、在应用程序只需要访问要的对象,而不需要访问与他关联的对象的场景下,加载与之...

    Hibernate教程

    20.1.1. 操作延迟加载的关联 20.1.2. 调整抓取策略(Tuning fetch strategies) 20.1.3. 单端关联代理(Single-ended association proxies) 20.1.4. 实例化集合和代理(Initializing collections and proxies) ...

    Hibernate+中文文档

    19.1.1. 操作延迟加载的关联 19.1.2. 调整抓取策略(Tuning fetch strategies) 19.1.3. 单端关联代理(Single-ended association proxies) 19.1.4. 实例化集合和代理(Initializing collections and proxies) ...

    hibernate3.2中文文档(chm格式)

    19.1.1. 操作延迟加载的关联 19.1.2. 调整抓取策略(Tuning fetch strategies) 19.1.3. 单端关联代理(Single-ended association proxies) 19.1.4. 实例化集合和代理(Initializing collections and proxies) ...

    HibernateAPI中文版.chm

    19.1.1. 操作延迟加载的关联 19.1.2. 调整抓取策略(Tuning fetch strategies) 19.1.3. 单端关联代理(Single-ended association proxies) 19.1.4. 实例化集合和代理(Initializing collections and proxies) ...

    Hibernate实战(第2版 中文高清版)

     13.1.6 通过拦截延迟加载   13.2 选择抓取策略   13.2.1 批量预抓取数据   13.2.2 通过子查询预抓取集合   13.2.3 通过联结即时抓取   13.2.4 给二级表优化抓取   13.2.5 优化指导方针   13.3 高速...

    Hibernate 中文 html 帮助文档

    19.1.1. 操作延迟加载的关联 19.1.2. 调整抓取策略(Tuning fetch strategies) 19.1.3. 单端关联代理(Single-ended association proxies) 19.1.4. 实例化集合和代理(Initializing collections and proxies) ...

    hibernate 教程

    延迟初始化(延迟加载)(Lazy Initialization) 6.6. 集合排序(Sorted Collections) 6.7. 使用<idbag><br>6.8. 双向关联(Bidirectional Associations) 6.9. 三重关联(Ternary Associations) 6.10....

    Hibernate中文详细学习文档

    19.1.1. 操作延迟加载的关联 19.1.2. 调整抓取策略(Tuning fetch strategies) 19.1.3. 单端关联代理(Single-ended association proxies) 19.1.4. 实例化集合和代理(Initializing collections and proxies) ...

    hibernate 体系结构与配置 参考文档(html)

    1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    19.1.1. 操作延迟加载的关联 19.1.2. 调整抓取策略(Tuning fetch strategies) 19.1.3. 单端关联代理(Single-ended association proxies) 19.1.4. 实例化集合和代理(Initializing collections and proxies) ...

    hibernate

    延迟初始化(延迟加载)(Lazy Initialization) 6.6. 集合排序(Sorted Collections) 6.7. 使用<idbag><br>6.8. 双向关联(Bidirectional Associations) 6.9. 三重关联(Ternary Associations) 6.10....

    hibernate3.04中文文档.chm

    20.1.1. 操作延迟加载的关联 20.1.2. 调整抓取策略(Tuning fetch strategies) 20.1.3. 单端关联代理(Single-ended association proxies) 20.1.4. 实例化集合和代理(Initializing collections and proxies...

    Hibernate参考文档

    19.1.1. 操作延迟加载的关联 19.1.2. 调整抓取策略(Tuning fetch strategies) 19.1.3. 单端关联代理(Single-ended association proxies) 19.1.4. 实例化集合和代理(Initializing collections and proxies) ...

    hibernate 框架详解

    目录 前言 1.... 2.... 1. 在Tomcat中快速上手 ... 1.1. 开始Hibernate之旅 ... 操作延迟加载的关联 20.1.2. 调整抓取策略(Tuning fetch strategies) 20.1.3. 单端关联代理(Single-ended association proxies) ...

    mybatis_day01.docx

    增加了程序员的一些操作,但是带来了设计上的灵活,并且也是支持hibernate的一些特性,如延迟加载,缓存和映射等;对数据库的兼容性比hibernate差。移植性不好,但是可编写灵活和高性能的sql语句。

Global site tag (gtag.js) - Google Analytics