简体   繁体   English

避免Hibernate LazyInitializationExceptions的策略

[英]Strategies to avoid Hibernate LazyInitializationExceptions

Are there any other ways to avoid LazyInitializationExceptions in a Hibernate web application besides using the OpenSessionInView pattern? 除了使用OpenSessionInView模式之外,还有其他方法可以避免Hibernate Web应用程序中的LazyInitializationExceptions吗? Are there any downsides to using OpenSessionInView? 使用OpenSessionInView有任何缺点吗?

When working on our web applications, we usually decide beforehand which objects/fields will be needed in the view pages and make sure that all the objecs are properly initialized from the model before dispatching to the view. 在处理我们的Web应用程序时,我们通常会事先决定在视图页面中需要哪些对象/字段,并确保在分派到视图之前从模型中正确初始化了所有对象。

This can be accomplished in (at least) three ways: 这可以通过(至少)三种方式实现:

  1. fetching properties using eager strategy (ie with FetchMode.JOIN , if you're using the Criteria API ) 使用急切策略获取属性(即使用FetchMode.JOIN ,如果您使用的是Criteria API
  2. explicitly initializing properties (ie with Hibernate.initialize(property) ) 显式初始化属性(即使用Hibernate.initialize(property)
  3. implicitly initializing properties by calling the appropriate property accessor 通过调用适当的属性访问器隐式初始化属性

About the downsides of OpenSessionInView, have you checked out this page? 关于OpenSessionInView的缺点,你有没看过这个页面?

Typically the best way to handle the problem, without making a global decision to do eager fetching; 通常是处理问题的最佳方式,而不是做出全球决定进行急切的提取; is to use the "fetch" keyword in conjuction with the hql query. 是使用“fetch”关键字与hql查询结合使用。

From http://www.hibernate.org/hib_docs/reference/en/html/queryhql-joins.html 来自http://www.hibernate.org/hib_docs/reference/en/html/queryhql-joins.html

In addition, a "fetch" join allows associations or collections of values to be initialized along with their parent objects, using a single select. 此外,“fetch”连接允许使用单个select来初始化值的关联或集合及其父对象。 This is particularly useful in the case of a collection. 这在集合的情况下特别有用。 It effectively overrides the outer join and lazy declarations of the mapping file for associations and collections. 它有效地覆盖了关联和集合的映射文件的外连接和延迟声明。 See Section 19.1, “Fetching strategies” for more information. 有关更多信息,请参见第19.1节“获取策略”。

from Cat as cat inner join fetch cat.mate left join fetch cat.kittens 来自Cat作为cat inner join fetch cat.mate left join fetch cat.kittens

Switch to JBoss Seam. 切换到JBoss Seam。

The Seam framework is well architected by the guys that developed Hibernate. Seam框架由开发Hibernate的人很好地构建。

Even with the Open Session in View, you may still have some problems. 即使使用Open View in View,您仍可能遇到一些问题。 Depending on how complex your web application is, Open Session In View does not handle all cases. 根据Web应用程序的复杂程度,Open Session In View不会处理所有情况。 I've had issues with displaying data as well (in the UI) as fetching entities during a quartz job such as sending an email. 我在显示数据时(在UI中)也遇到了在石英作业(例如发送电子邮件)期间获取实体的问题。

Hibernate already intelligently fetches data, changing the fetch mode will result in performance degradation. Hibernate已经智能地获取数据,更改获取模式将导致性能下降。 Not only that, but you are getting away from convention and will muck up your project with extraneous configuration details. 不仅如此,您还远离惯例,并且会通过无关的配置细节来破坏您的项目。

Walter 沃尔特

The Oracle Java tutorials point out that "Enterprise beans support transactions, the mechanisms that manage the concurrent access of shared objects." Oracle Java教程指出“Enterprise Bean支持事务,即管理共享对象并发访问的机制。” So, in order to handle the Lazy Fetch issues I create a Stateless Java Session Bean and then get all of the sub classes I need before returning from the method. 因此,为了处理Lazy Fetch问题,我创建了一个无状态Java会话Bean,然后在从方法返回之前获取我需要的所有子类。 Oracle has also referred to this as a "Session Façade" core J2EE pattern. Oracle还将此称为“SessionFaçade”核心J2EE模式。 These practices seem like much better application practices than some of the other ones mentioned. 这些实践似乎比其他一些提到的应用实践更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM