OneToMany, orphanRemoval=false, target entity is not removed in certain case
Description
Attachments
- 10 Sep 2013, 01:05 PM
Activity
Christian Beikov January 21, 2025 at 11:23 AM
Removing an entity from a unowned collection (i.e. one that uses mappedBy
) does not cause any changes to the collection relationship. You have to remove the element via EntityManager.remove()
or set remove the relationship on the owning side i.e. set Book#author
to null
and merge that entity.
Steve Ebersole October 28, 2015 at 3:24 AM
As part of verifying that this issue affects 5.0, please just set the "Affects version". Leave the "verify-affects-5.0" label and leave the issue in "Awaiting Response" status; these are critical for us to be able to track these verifications and triage them. Thanks.
Steve Ebersole October 27, 2015 at 7:16 PM
This bug report does not indicate that the reported issue affects version 5.x. Versions prior to 5.x are no longer maintained. It would be a great help to the Hibernate team and community for someone to verify that the reported issue still affects version 5.x. If so, please add the 5.x version that you verified with to the list of affected-versions and attach the (preferably SSCCE) test case you used to do the verification to the report; from there the issues will be looked at during our triage meetings.
For details, see http://in.relation.to/2015/10/27/great-jira-cleanup-2015/
We have a OneToMany relationship (List) with cascade ALL and orphanRemoval = false
If we remove a target entity from the List we expect the entity to be removed from the collection but not from the DB.
It works pretty well, when working with a managed parent.
However, if you remove the target entity from the list while the parent is detached and later on you merge the parent, then the target entity is neither removed from the list nor from the database.
See test case attached (you can play with the removeBookWhileAuthorIsDetached flag to make the test pass/fail)
Note that with oprhanRemoval = true, if you remove the target entity from the list while the parent is detached and later on you merge the parent, then the target entity is properly removed from the list and from the database.