I’m facing a LazyInitializationException
when I’m trying to unproxy a relation having FetchType.LAZY
. this issue is random and cannot be reproduced, I happened to find the next exception in the server logs:
2025-01-24 07:00:35,710 ERROR [org.hibernate.proxy.AbstractLazyInitializer] (default-threads - 30) Initialization failure [com.xyz.MyEntity#null]: java.lang.IllegalArgumentException: id to load is required for loading
at org.hibernate.event.spi.LoadEvent.<init>(LoadEvent.java:96) [hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.event.spi.LoadEvent.<init>(LoadEvent.java:64) [hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.internal.SessionImpl.recycleEventInstance(SessionImpl.java:1080) [hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.internal.SessionImpl.immediateLoad(SessionImpl.java:1004) [hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.proxy.AbstractLazyInitializer.permissiveInitialization(AbstractLazyInitializer.java:214) [hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167) [hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:310) [hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at com.xyz.Utility.unwrapProxy(Utility.java:50) ~[implementation.jar:?]
The same process is executed every day but it only fails once or twice a month.
The is the code of unproxy method:
if (object instanceof HibernateProxy) {
HibernateProxy hibernateProxy = (HibernateProxy) object;
LazyInitializer initializer = hibernateProxy.getHibernateLazyInitializer();
return initializer.getImplementation(); //this is line 50 in the exception
}
I copied this from Vlad’s thread
I don’t have a reproducible scenario, I tried to debug the issue, I found out that a proxy object cannot have a LazyInitializer with Id null
when creating the object which means that the object is made with a valid identifier. Also, there is no other exceptions logged at the time when this exception occured.
My question: What can cause the LazyInitializer registered on a hibernate proxy to have an Id null?