hibernate:LazyInitializationException:无法初始化代理 [英] hibernate: LazyInitializationException: could not initialize proxy
问题描述
以下是基本代码:
int startingCount = sfdao.count();
sfdao.create(sf);
SecurityFiling sf2 = sfdao.read(sf.getId());
sfdao.delete(sf);
int endingCount = sfdao.count();
assertTrue(startingCount == endingCount);
assertTrue(sf.getId()。longValue()== sf2.getId()。longValue());
assertTrue(sf.getSfSubmissionType()。equals(sf2.getSfSubmissionType()));
assertTrue(sf.getSfTransactionNumber()。equals(sf2.getSfTransactionNumber()));
它在第三个assertTrue失败,它试图将sf中的值与sf2中的相应值进行比较。以下是例外情况:
org.hibernate.LazyInitializationException:无法初始化代理 - 没有会话
在org.hibernate。 proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:86)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:140)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer。 invoke(JavassistLazyInitializer.java:190)
at com.freightgate.domain.SecurityFiling _ $$ _ javassist_7.getSfSubmissionType(SecurityFiling _ $$ _ javassist_7.java)
at com.freightgate.dao.SecurityFilingTest.test(SecurityFilingTest。在sun.reflect.NativeMethodAccessorImpl.invoke0(本地方法)
$ sunfound.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
在sun.reflect.DelegatingMethodAccessorImpl
。在com.intellij.rt.execution.junit.JUnitStarter.main调用(DelegatingMethodAccessorImpl.java:25)
(JUnitStarter.java :40)
问题在于您试图访问分离的对象中的集合。在将集合访问到当前会话之前,您需要重新附加对象。你可以通过
session.update(object);
使用 lazy = false
并不好解决方案,因为你抛弃了休眠的懒惰初始化功能。当 lazy = false
时,集合在请求对象的同时加载到内存中。这意味着如果我们有一个包含1000个项目的集合,尽管我们要访问它们,但它们全都会被加载到内存中。这是不好的。
请阅读文章它解释了问题,可能的解决方案以及为什么以这种方式实施。此外,要了解会话和交易,您必须阅读这篇其他文章。
Here's one that has me perplexed. I'm trying to implement a basic Hibernate DAO structure, but am having a problem.
Here's the essential code:
int startingCount = sfdao.count();
sfdao.create( sf );
SecurityFiling sf2 = sfdao.read( sf.getId() );
sfdao.delete( sf );
int endingCount = sfdao.count();
assertTrue( startingCount == endingCount );
assertTrue( sf.getId().longValue() == sf2.getId().longValue() );
assertTrue( sf.getSfSubmissionType().equals( sf2.getSfSubmissionType() ) );
assertTrue( sf.getSfTransactionNumber().equals( sf2.getSfTransactionNumber() ) );
It fails on the third assertTrue where it's trying to compare a value in sf to the corresponding value in sf2. Here's the exception:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:86)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:140)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
at com.freightgate.domain.SecurityFiling_$$_javassist_7.getSfSubmissionType(SecurityFiling_$$_javassist_7.java)
at com.freightgate.dao.SecurityFilingTest.test(SecurityFilingTest.java:73)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:40)
The problem is that you are trying to access a collection in an object that is detached. You need to re-attach the object before accessing the collection to the current session. You can do that through
session.update(object);
Using lazy=false
is not a good solution because you are throwing away the Lazy Initialization feature of hibernate. When lazy=false
, the collection is loaded in memory at the same time that the object is requested. This means that if we have a collection with 1000 items, they all will be loaded in memory, despite we are going to access them or not. And this is not good.
Please read this article where it explains the problem, the possible solutions and why is implemented this way. Also, to understand Sessions and Transactions you must read this other article.
这篇关于hibernate:LazyInitializationException:无法初始化代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!