简体   繁体   English

事务内的org.hibernate.LazyInitializationException

[英]org.hibernate.LazyInitializationException within Transaction

Currently using slightly older Hibernate (3.3.2.GA) - and getting org.hibernate.LazyInitializationException within Transaction boundaries. 当前使用稍旧的Hibernate(3.3.2.GA)-并在Transaction边界内获取org.hibernate.LazyInitializationException。 I confirmed that the calling method is marked @Transactional , plus stack trace at the time of exception is showing it too: org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) 我确认调用方法被标记为@Transactional ,加上异常时的堆栈跟踪也显示了它: org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)

Still, an attempt to iterate over @CollectionOfElements promptly throws the LazyInitializationException with " no session or session was closed ". 尽管如此,尝试遍历@CollectionOfElements立即引发LazyInitializationException并显示“没有会话或会话已关闭”。

While debugging it, I see that Hibernate proxy on the relevant collection has a session of null virtually as soon as it is loaded by Hibernate. 调试它时,我看到相关集合上的Hibernate代理实际上在Hibernate加载后就具有null会话。 Is that something to worry about? 有什么需要担心的吗? How can I track this down and resolve it - in other words, while debugging, what's the best way to track down what is happening to the session? 我如何跟踪并解决它-换句话说,在调试时,跟踪会话正在发生的最佳方法是什么?

Obviously I understand that changing the collection to Eager fetching will resolve the issue, but I rather figure out why Lazy Load is not working since this (potentially) large collection is not needed in most scenarios. 显然,我知道将集合更改为Eager Fetch可以解决此问题,但是我想弄清楚为什么懒惰加载不起作用,因为在大多数情况下不需要此(可能是)大型集合。

Thank You! 谢谢!

adir1 I had exactly the same problem. 我有完全相同的问题。 In my case the issue was caused by the fact that the instance which contained the lazy-loaded collection had been manually evicted before (all within transaction boundaries). 在我的情况下,此问题是由以下事实引起的:包含延迟加载的集合的实例之前已被手动逐出(均在事务边界内)。

Regarding the age of this question, for posterity: 关于这个问题的年龄,为了后代:

The same problem happened for me because I kept entities cached in memory. 我遇到了同样的问题,因为我将实体缓存在内存中。 The exception was thrown as soon as accessing those entities in a later transaction - different from the one during which the entities were first collected. 在以后的事务中访问这些实体后立即引发异常-与第一次收集实体时不同。 Hence the lost session problem. 因此,丢失会话的问题。

Solved by setting up proper 2nd level caching. 通过设置适当的二级缓存来解决。

My guess is that when you return from the method fetching the entity and start iterating through it you're already at different transactional contexts, thus leading to the lazy initialization problem; 我的猜测是,当您从获取实体的方法中返回并开始对其进行迭代时,您已经处在不同的事务上下文中,从而导致了惰性初始化问题。 if you're using a DAO try setting both of these methods to the same transaction (ie setting them as REQUIRED). 如果您使用的是DAO,请尝试将这两种方法都设置为同一事务(即,将它们设置为REQUIRED)。

I might be totally mistaken though. 我可能完全误会了。 I've just tested this with EJB3 + JPA and got the lazy initialization problems you described when the method fetching the entity was at a different transactional context. 我刚刚使用EJB3 + JPA对此进行了测试,并得到了您在获取实体的方法在不同的事务上下文中时描述的惰性初始化问题。

EDIT: Like baba said in his comment, a better solution would be either eager fetching through HQL / Criteria (fetch join clause in HQL) or, less preferably, using Hibernate.initialize before returning it. 编辑:就像巴巴在他的评论中说的那样,一个更好的解决方案要么是急于通过HQL / Criteria(HQL中的fetch join子句)来获取,要么最好是在返回之前使用Hibernate.initialize。

暂无
暂无

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

相关问题 Hibernate org.hibernate.LazyInitializationException:无法初始化 - Hibernate org.hibernate.LazyInitializationException: could not initialize Hibernate抛出org.hibernate.LazyInitializationException - Hibernate threw org.hibernate.LazyInitializationException org.hibernate.LazyInitializationException(Spring / Hibernate) - org.hibernate.LazyInitializationException (Spring/Hibernate) org.hibernate.LazyInitializationException关联集合 - org.hibernate.LazyInitializationException associated collection 在TestNGTests上获取org.hibernate.LazyInitializationException - Getting org.hibernate.LazyInitializationException on a TestNGTests 方法抛出“org.hibernate.LazyInitializationException”异常 - Method threw 'org.hibernate.LazyInitializationException' exception 如何解决org.hibernate.LazyInitializationException? - how can to resolve org.hibernate.LazyInitializationException? (org.hibernate.LazyInitializationException)org.hibernate.LazyInitializationException:无法初始化代理-没有会话 - (org.hibernate.LazyInitializationException) org.hibernate.LazyInitializationException: could not initialize proxy - no Session Spring和Hibernate Web应用程序:如何解决'org.hibernate.LazyInitializationException? - Spring and Hibernate web application: how to resolve 'org.hibernate.LazyInitializationException? Hibernate ManyToMany 方法抛出“org.hibernate.LazyInitializationException”异常 - Hibernate ManyToMany Method threw 'org.hibernate.LazyInitializationException' exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM