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.

Activity

Show:

Christian Beikov April 8, 2022 at 1:23 PM

Since no test case was provided for years, I'll close this issue as out of date. I also believe it will most probably not be an issue any more in 6.0.
If this still is an issue for you, please create a new issue with a test case that reproduces the problem.

Vlad Mihalcea November 30, 2018 at 8:05 AM

We need a replicating tets case for this issue. Check out this template for more details.

Also, I made it a Major since this is not a Blocker. Eagerly-loaded collections are going to cause you terrible performance issues even after this bug gets fixed, so keep that in mind.

Out of Date

Details

Assignee

Reporter

Labels

Worked in

Components

Priority

Created September 1, 2018 at 6:20 PM
Updated April 8, 2022 at 1:23 PM
Resolved April 8, 2022 at 1:23 PM