We have a parent-child relation with
on the parent-side and
on the children-side. So basically the children cannot exist without a parent.
If we now create a parent with children, add the entities to the entity manager, remove one child and flush those changes, we end up with very different results depending on if we used persist or merge and flushing or not in the first place.
persist works as expected (flushed or not)
merge works only if the initial parent was flushed
If we however merge without flushing, it depends on whether we clear the parent on the child side:
If the parent is not cleared, the orphan child is inserted and effectively not removed as orphanRemoval suggests
If the parent is cleared, Hibernate tries to insert the orphan child into the database and fails with the null reference to the parent
Since both cases work perfectly with persist (instead of merge), we would expect the very same behavior after an initial merge? Is this expectation justified? Or are we doing something wrong?
This may be related with respectively HHH-9571, at least the DeleteOneToManyOrphansTest test case within that defect has some similarities. That case however is using persist and flush which works perfectly in our case, that's why we opened a new issue on this one.
You may find another very small stripped down test project for these cases on Github https://github.com/abenneke/sandbox/tree/master/hibernate-flush-remove