hibernate的懒加载问题时常会困扰着我们, 今天发现了hibernate自身也提供一些解决办法
Hibernate.initialize(company.getUsers()); 这样就加载了users集合
但不支持递归加载
it is not guaranteed that the elements INSIDE the collection will be initialized/materialized
假如这时session关闭了 会报错
真正解决懒加载还是应该在类的设计 xml文件的配置上下功夫
源码:
/**
* Force initialization of a proxy or persistent collection.
* <p/>
* Note: This only ensures intialization of a proxy object or collection;
* it is not guaranteed that the elements INSIDE the collection will be initialized/materialized.
*
* @param proxy a persistable object, proxy, persistent collection or <tt>null</tt>
* @throws HibernateException if we can't initialize the proxy at this time, eg. the <tt>Session</tt> was closed
*/
public static void initialize(Object proxy) throws HibernateException {
if ( proxy == null ) {
return;
}
else if ( proxy instanceof HibernateProxy ) {
( ( HibernateProxy ) proxy ).getHibernateLazyInitializer().initialize();
}
else if ( proxy instanceof PersistentCollection ) {
( ( PersistentCollection ) proxy ).forceInitialization();
}
}
/**
* Check if the proxy or persistent collection is initialized.
*
* @param proxy a persistable object, proxy, persistent collection or <tt>null</tt>
* @return true if the argument is already initialized, or is not a proxy or collection
*/
public static boolean isInitialized(Object proxy) {
if ( proxy instanceof HibernateProxy ) {
return !( ( HibernateProxy ) proxy ).getHibernateLazyInitializer().isUninitialized();
}
else if ( proxy instanceof PersistentCollection ) {
return ( ( PersistentCollection ) proxy ).wasInitialized();
}
else {
return true;
}
}
分享到:
相关推荐
hibernate懒加载策略.dochibernate懒加载策略.doc
hibernate抓取策略和懒加载案例,供大家下载查看。大家都知道,hibernate都设计到一个优化问题。
深入理解hibernate懒加载技术,正确使用懒加载
Hibernate延迟加载以及利用Spring 大家看看 参考一下
hibernate延迟加载解决 延迟加载的具体解决方法
主要介绍了浅谈解决Hibernate懒加载的4种方式,具有一定借鉴价值,需要的朋友可以参考下。
解决org.hibernate.LazyInitializationException: could not initialize proxy - no Session...
去除预加载的Viewpager,加载第一页时不会默认加载第二页,
hibernate的中文问题的解决方案
详细介绍hibernate延迟加载,对hibernate初学者有一定的帮助
Hibernae 的延迟加载是一个非常常用的技术,实体的集合属性默认会被延迟加载,实体所关联的实体默认也会被延迟加载。Hibernate 通过这种延迟加载来降低系统的内存开销,从而保证 Hibernate 的运行性能。
任何框架都有自身的能力范围,就拿Hibernate和Mybatis这两个ORM框架来说。 Hibernate封装了很多有用的API给开发者,降低了操作数据库的难度和复杂度,同时也减少了模板代码的数量,但Hibernate留给开发者可操作的...
Hibernate提供的查询接口或其方法 hibernate综合查询解决方案
hibernate错误解决方案 (如:org.springframework.orm.ObjectRetrievalFailureException问题等一系列常见问题)
Hibernate多数据库支持解决方案 一、 去掉数据库级的外键关联 二、 如何动态加载.hbm.xml文件 三、 配置文件的加载 四、 大字段处理 五、 hibernate主键生成器(数值型主键操作方法) 六、 Oracle 转换 SQL Server ...
hibernate常见错误解决方案
hibernate实现动态表查询的多种解决方案.自行整理的动太实现方案,总有一个适合你.
jsp中关于Hibernate延时加载的问题,实例源代码,详解都有