Envers fires additional queries for loading referenced entities

Description

Hi there,

we shortly updated our hibernate and hibernate envers versions and experienced an (I believe) unwanted behaviour:

Assume the following Integration Test using Spring Data Repositories:

Expected behavior:
As the id of the "OtherEntity" reference is held in the proxy, Envers should be able to fill it without additional queries.

Current behavior:
Envers fires a SELECT query to retrieve the "OtherEntity" reference from the database.

This is bad for various reasons in my opinion:
1. it causes unnecessary work on the database due to the fact, that there are multiple additional queries fired which are not needed at all
2. it makes EntityManager.getReference() method totally useless. This method is great for getting a reference without the need to load the object from the Database to store it in a relationship. If there is a select now every time I save an object, this usecase is totally gone
3. if there is no transaction going on at the point of saving the entity, it is even worse because a LayzInitializationException is thrown in this case.

Additional information:
I tested with all versions from 5.4.10 (which we previously worked with) to the current 5.4.20. The described behaviour shows up in 5.4.11 and persists from this point on
I looked through the JIRA issues related to hibernate-envers 5.4.11 and found https://hibernate.atlassian.net/browse/HHH-13760 which could be related, but I am not sure about this

I choosed major priority because I see this as a breaking change introduced in a minor version which should probably not occur and because it prevents us from upgrading.

If you see a possibility to workaround this issue I would be really happy to hear about it.

Environment

Spring Boot 2.3, Hibernate-Envers 5.4.11.Final (and higher)

Assignee

Chris Cranford

Reporter

Christian Schwerdtfeger

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Worked in

5.4.10

Components

Affects versions

Priority

Major
Configure