LazyInitializationException: id to load is required for loading

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?

What do the entity mappings look like? How are these lazy proxies stored before initialization? Are you using a second level cache?

If you are not able to reproduce the issue on your side, it’s going to be impossible for us to understand where the problem is coming for. The error is quite self-explanatory: one of your entity proxies have a null entity identifier value and, when you try initializing them, the exception is thrown.

I’m sorry if I can’t provide more help, but there are too many variables at play here and we don’t have nearly enough information.

This is my entity mapping:

@OneToMany(targetEntity = com.xyz.MyEntity.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "otherEntityKey",
			orphanRemoval = true)

private Set<MyEntity> compEnt = new java.util.HashSet<>();

Before init, the proxies are stored automatically by hibernate, I just do
ParentEntity p = em.find(ParentEntity.class, 1L);
on the parent entity.
The second-level cache is activated, but I’m not using it for this entity or the parent.

As I said in the original post, the proxy I’m trying to initialize cannot be created with a null identifier.If it was the case, unproxying the object will just return null.

My question here is: is there any rare cases when this happened to someone and what was the issue, I know that this is very difficult to investigate.

Thanks,

@vlad @beikov, any thoughts, please?

You seem to be using ORM 5.4 which is unsupported. Also see Maintenance Policy - Hibernate. If you have this same problem with the latest stable version i.e. ORM 6.6 then we can talk further.