EntityNotFoundException when loading non-proxy entities with circular dependencies

Description

I have a some entities sharing a common ancestor class annotated with:

 

Subclasses of these entities have self-referencing or otherwise circular @ManyToOne relations to other entities. So when loading one object, some others need to be recursively loaded.

Under some conditions this results in an EntityNotFoundException for an entity that definitively exists.

Based on the stacktrace, Hibernate recursively loads entities either from 2nd level cache or from datasource, as expected.

 

The behaviour seems to be triggered by the presence (or absence) of some entities in the 2nd level cache, so it does not always happen. Contrary to what I initially thought, this does not seem to be a race condition: the cache evictions do not need to happen while Hibernate is loading for the problem to happen.

This also seems to depend on the state of the fetch queue, so the order of loading matters too.

I’ve observed in production that this only seems to happen for entities mapped with a @SecondaryTable but this might be a red herring (I tried debugging the problem but my understanding of Hibernate inner’s working is too limited).

I’ll try making a pull-request with a unit test reproducing the problem.

This is a follow-up to:

Activity

Show:

Guillaume Toison April 9, 2024 at 7:17 PM

Another (simpler) test reproducer:

Guillaume Toison April 3, 2024 at 2:02 PM

Issue reproducer:

Fixed

Details

Assignee

Reporter

Worked in

Components

Sprint

Fix versions

Affects versions

Priority

Created April 3, 2024 at 1:40 PM
Updated April 12, 2024 at 8:35 PM
Resolved April 12, 2024 at 8:35 PM