问题描述
我在对象中使用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:
- ObjectA
- ObjectB
- ObjectC
ObjectA包含ObjectB作为FetchType = EAGER
ObjectB包含ObjectC作为FetchType = LAZY
ObjectA includes ObjectB as FetchType=EAGERObjectB 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.我发现了类似的链接 - 我是在我的代码中使用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。但是当我试图使用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 :
- ObjectB.getObjectC或
- ObjectA.getObjectB.getObjectC?
,您将获得LazyInitializationException异常。
and you get an exception of LazyInitializationException.
Hibernate文档说..
Hibernate Documentation says that..
有时候,代理或集合需要在关闭
之前初始化会话。您可以通过调用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 - 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!