Potential bug with Hibernate7 on StatefulPersistenceContexte::replaceCollection

Hi everyone.
I may have found an issue on hibernate 7 (more precisely, I am using version 7.1.24.Final), that was not there with Hibernate 6.6.42.Final.

I made a reproduction case you can find on my github here:

Simply run a “mvn test” and you’ll have the issue reproduced.

But let me explain approximatively what I understood:

When persisting an entity that cascades (CascadeType.ALL) to a Set of child entities, each of which itself cascades to its own (initially empty) nested Set collection, the following sequence throws an internal HibernateException: No collection for replacement found: . instead of completing normally:

  • Persist the parent entity (which provoke the cascade)
  • Provoke a flush
  • Merge the already-managed child (I know this is not very clean, but when I do that with hibernate 6.6.42, it works, so well…).
    => the exception “No collection for replacement found: com.example.hibernateissue..” occurs

From what I found, the issue comes from StatefulPersistenceContexte.java, that has a new way to handle collection replacement. More precisely, inside “addCollectionByKey”, when doing the “put”, it returns “null” while I expected it to find my old collection.

The fact that I do a non-conventional merge on an already managed entity makes me think that this is not really a bug. Yet, I wanted to share it to you so you can see by yourself and make a decision about it.

Thanks for reading and for your feedbacks (if any) :slight_smile:

Please try to create a reproducer with our test case template and if you are able to reproduce the issue, create a bug ticket in our issue tracker and attach that reproducer.

No problem. I will do this during the weekend or early next week. I am short on time right now.

If needed: