LazyInitializationException with hibernate.enable_lazy_load_no_trans

Description

Hi,
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?
Thanks.
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]

Environment

Java 8 / tomcat 8.5.24 /spring 4.3.1

Activity

Show:
Selaron
June 26, 2018, 6:24 AM
Edited

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).

Süleyman Vurucu
June 26, 2018, 4:59 PM

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.

jean-pierre jean-pierre
June 27, 2018, 6:16 AM
Edited

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
http://ankursinghal86.blogspot.com/2014/08/hibernates-and-lazyinitializationexcept.html

@Hibernate team : Maybe it's time to summarize all the cases that can cause this exception.

Selaron
June 27, 2018, 2:10 PM
Edited

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?

edit:
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.

jean-pierre jean-pierre
June 27, 2018, 3:51 PM

@Selaron, thanks for your tip. You are right there is a serialization that break the lazy load of the relation.

Assignee

Yoann Rodière

Reporter

jean-pierre jean-pierre

Fix versions

Labels

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

backportDecision

None

Worked in

5.2.10

Components

Affects versions

Priority

Major
Configure