当会话未关闭时,lazyinitializationexception [英] lazyinitializationexception when session is not closed

查看:90
本文介绍了当会话未关闭时,lazyinitializationexception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在对象中使用Hibernate,即使会话未关闭,也会获得lazyinitializationexception。

I am using Hibernate in my object and getting lazyinitializationexception even when session is not closed.

这里是有问题的对象的关系。有3个对象:

Here is the relation of the objects in issue. There are 3 Objects:


  1. ObjectA

  2. ObjectB

  3. ObjectC

ObjectA包含ObjectB作为FetchType = EAGER
ObjectB包含ObjectC作为FetchType = LAZY

ObjectA includes ObjectB as FetchType=EAGER ObjectB includes ObjectC as FetchType=LAZY

我们正在获取ObjectA。所以,由于渴望获取类型,它是自动提取ObjectB。
但是当我试图使用ObjectB来获取ObjectC时,它会给出这个错误。

We are fetching ObjectA. So, because of eager fetch type, it is automatically fetching ObjectB. But when I am trying to fetch ObjectC using ObjectB, it is giving this error.

代码太大,专有。所以,不能共享代码。

The code is too big and proprietary. So, can't share the code.

注意:
1.所有这些操作只在一个事务中完成。
2.我检查了日志,发现会话没有关闭。
3.我发现了类似的链接 - JPA和Hibernate中的LazyInitializationException 我是在我的代码中使用annotationDriven标签也是在这个链接中建议的。

Note: 1. All these operations are being done inside one transaction only. 2. I checked the logs and found out that session is not being closed. 3. I found similar link - LazyInitializationException in JPA and Hibernate I am using annotationDriven tag also in my code as suggested in this link.

我不知道为什么会发生。

I am not getting if why is it happening.

推荐答案

我的理解是否正确?


ObjectA包含ObjectB作为FetchType = EAGER ObjectB包含ObjectC
作为FetchType = LAZY

ObjectA includes ObjectB as FetchType=EAGER ObjectB includes ObjectC as FetchType=LAZY

我们正在获取ObjectA。所以,由于渴望获取类型,它是
自动提取ObjectB。但是当我试图使用ObjectB来获取ObjectC
时,它会提供此错误。

We are fetching ObjectA. So, because of eager fetch type, it is automatically fetching ObjectB. But when I am trying to fetch ObjectC using ObjectB, it is giving this error.

ObjectA.getObjectB = OK?我的意思是没有异常抛出或没有错误?

ObjectA.getObjectB = OK ? i mean no exception thrown or no error right?

然后当您通过ObjectB访问ObjectC时:

then when you access ObjectC by ObjectB by :


  1. ObjectB.getObjectC或

  2. ObjectA.getObjectB.getObjectC?

,您将获得LazyInitializationException异常。

and you get an exception of LazyInitializationException.

Hibernate文档说..

Hibernate Documentation says that..


如果
未初始化集合或代理访问超出$ b范围,则Hibernate将抛出LazyInitializationException异常$ b会话,即当拥有该集合的实体或对代理的
引用处于分离状态时。

A LazyInitializationException will be thrown by Hibernate if an uninitialized collection or proxy is accessed outside of the scope of the Session, i.e., when the entity owning the collection or having the reference to the proxy is in the detached state.

有时候,代理或集合需要在关闭
之前初始化会话。您可以通过调用cat.getSex()或
cat.getKittens()。size()来强制初始化。但是,这可能会让
混淆到代码的读者身上,而且通用代码是不方便的。

Sometimes a proxy or collection needs to be initialized before closing the Session. You can force initialization by calling cat.getSex() or cat.getKittens().size(), for example. However, this can be confusing to readers of the code and it is not convenient for generic code.

静态方法Hibernate.initialize()和
Hibernate.isInitialized(),为应用程序提供一个方便的
使用延迟初始化集合或代理的方式。
Hibernate.initialize(cat)将强制初始化一个代理,
cat,只要其Session仍然是打开的。 Hibernate.initialize(
cat.getKittens())对于小猫的收集具有相似的效果。

The static methods Hibernate.initialize() and Hibernate.isInitialized(), provide the application with a convenient way of working with lazily initialized collections or proxies. Hibernate.initialize(cat) will force the initialization of a proxy, cat, as long as its Session is still open. Hibernate.initialize( cat.getKittens() ) has a similar effect for the collection of kittens.

from - <一个href =https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/performance.html#performance-fetching-initialization =nofollow> https://docs.jboss。 org / hibernate / orm / 3.3 / reference / en / html / performance.html#performance-fetching-initialization

from - https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/performance.html#performance-fetching-initialization

你有两个选项

更改 FetchType = LAZY FetchType = EAGER 关于ObjectB之间的关系和ObjectC

Change FetchType=LAZY to FetchType=EAGER on your relationship between ObjectB and ObjectC

或使用 Hibernate.initialize(ObjectC);
在事务之前初始化objectC

or use Hibernate.initialize(ObjectC); to initialize the objectC before the transaction ends..

希望这将有助于您..

hope this will help you..

这篇关于当会话未关闭时,lazyinitializationexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆