We have setup hibernate.enable_lazy_load_no_trans to true in persistence.xml and we have LazyInitializationException thrown when we access a lazy @ManyToOne relation.
When the code runs AbstractLazyInitializer.initialize() the allowLoadOutsideTransaction property is set to false and initialized too.
Could you give me some hints to try to identify what's happend?
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:146) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:259) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:73) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at com.xxxxx.dal.commun.model.Formule_$$jvst16c_5f.getProgrammeId(Formule$$_jvst16c_5f.java) ~[classes/:na]
Java 8 / tomcat 8.5.24 /spring 4.3.1
Do you ever serialize and deserialize entities?
As AbstractLazyInitializer is not serializable, it forgets about the initial allowLoadOutsideTransaction = true.
edit 1: might be related if this is the root cause here.
edit 2: added a test case that reproduces the given StackTrace. Still not sure if it reproduces the actual reporters' issue.
(De)Serialization eventually happens when entities are stored in servlet container sessions (e.g. SessionScope or ViewScope in JSF environment).
Have this issue on all types of assosiations.
The inital load with te fetch is ok bit furthermore queries stucks wtih lazy init expetion or nullpointer if i enable hibernate.enable_lazy_load_no_trans in my persistence.xml.
Thanks Selaron for the test case. I will run it soon. For the moment, i try to use org.apache.commons.lang3.SerializationUtils to serialize an entity in a simple test case and the lazy load works fine with the deserialized entity.
AbstractLazyInitializer has some properties (like allowLoadOutsideTransaction which is used to manage lazy load in new session) that are not transient except the session property. So Serialization seems not to be a problem.
it seems that there may be other causes for LazyInitiliazationException as mentionned in this blog
@Hibernate team : Maybe it's time to summarize all the cases that can cause this exception.
For me, allowLoadOutsideTransaction is always false after (de)serialization even if it was true in the source object.
java version "1.8.0_152"
Java(TM) SE Runtime Environment (build 1.8.0_152-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.152-b16, mixed mode)
OS: Windows 8
Am I doing something special?
My Proxy is a org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.
It does serialization using an intermediate SerializableProxy instance which is created by:
and on deserialization read back by:
SerializableProxy has no allowLoadOutsideTransaction field and such its' state gets lost for me.
@Selaron, thanks for your tip. You are right there is a serialization that break the lazy load of the relation.