We're updating the issue view to help you get more done. 

Multiple Collection Loader/Initializers Lead to 11 Copies of Same Entity In a Collection

Description

While at work we found an issue with Hibernate when upgrading from Hibernate 4 to Hibernate 5 while switching from Spring Boot 1.5 to Spring Boot 2.x. When testing with the new Setup we found an issue regarding to how a collection is loaded. Switching to Spring Boot 2.0.4 which uses Hibernate 5.2.17, the issue was gone.

The problem itself was detected based on an eager loaded collection when loading a single element it had 11 elements in it but all were basically the same entity. We found the same problem with multiple collections once we know where to look at.

Debugging into Hibernate, it was found ,that the problem is introduced during the preparation of the batch loaders of the collection. For one collection the batch size is 10. Each collection initializer is modeled by a template initializer. While each initializer is created the code inserts a new instance to a 'global' instance that is a loader.

Since the template initializer + 10 batch initializer makes 11 and each contributes an result set processing instance. At the point the actual loading of (any) collection is commenced the initial (JDBC) result set is then being processed by each of the 11 loader contributed during initialization. Each of those loader will process the result set and look up for the entity in the cache. While the first of the eleven loader will not find a cached version every other 10 will and that is the reason why we see 11 elements being added to the eager fetched collection but all elements refer to the same entity.

While stepping through the code while this initialization happens, it was noticed that beside a collection initializer gets added every of the 10 + 1 times, an entity initilizer is added as well. The multiple entity initilizer have no effect as only one result is used if at all.

As I am currently not at work, I will fill in the blanks and add further information like stack traces and the correct source code pieces along with the entity configuration and a list of the evolved classes.

As this behavior makes it impossible to use Hibernate 5.3.x, we switched to Hibernate 5.2.17 which did not exhibit this behavior.

Environment

Windows 10

Status

Assignee

Unassigned

Reporter

Martin Kersten

Fix versions

None

Labels

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Feedback Requested

2018/11/30

Worked in

5.2.17

Feedback Requested By

Vlad Mihalcea

Components

Affects versions

5.3.1
5.3.5
5.3.3
5.3.2
5.3.6
5.3.4

Priority

Major