Entity contains wrong reference data on EntityManager.find()

Hi.

I have the problem that Hibernate returns an entity with “hallucinated” data, if entityManager.find() is called in a certain order.

A testcase can be found here: GitHub - haarli/hibernate-orm-6-testcase
(The database is filled with data from the import_new.sql file at the beginning)


Here is the data model:

Organization
objectId (primary key)
name
parentOrganization → Organization
predecessorOrganizations → List of Organization

User
objectId (primary key)
name
organization → Organization


That’s the concrete data:

ou_1 (Organization)
parentOrganization = null
predecessorOrganizations = ou_3

ou_2 (Organization)
parentOrganization = ou_1
predecessorOrganizations = empty

ou_3 (Organization)
parentOrganization = null
predecessorOrganizations = empty

user_1 (User)
organization = ou_2


If I now call

EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();

User user1 = entityManager.find(User.class, "user_1");
Organization ou3 = entityManager.find(Organization.class, "ou_3");
//ou3 suddenly has a parent organization -- not correct!

entityManager.getTransaction().commit();
entityManager.close();

within a transaction, the ou_3 suddenly has a parent organization ou_1. But in the DB, the value is NULL.

If I call

EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();

Organization ou3 = entityManager.find(Organization.class, "ou_3");
//ou3 has no parent organization -- correct

entityManager.getTransaction().commit();
entityManager.close();

in a new instance of EntityManger, ou_3 is returned correctly without a parent organization


This seems really weird to me. Any help is highly appreciated. Thank you

EDIT: This is about Hibernate 6.6. It used to work with Hibernate 5.6

Thanks for the report. It looks like there is a problem in detecting bidirectional associations. Can you please create a bug report in our issue tracker for this?

Thanks. I opened an issue: https://hibernate.atlassian.net/browse/HHH-19963

1 Like