jits_1998 wrote:
Well hibernate is doing what its being told. Its being asked to delete the object by calling remove on the parent, with inverse='false', and then adding it to another one, hence asking it to add a new child based on inverse='false' again.
Well, yes and no. I see why it's doing what it's doing, but I'm not convinced it's doing "what it's told" or "what it should", because:
1. The newParent.getChildren().add(group) call fails silently. I would have thought that at the very least Hibernate would complain about the object having been deleted. In fact, looking at the SQL it seems Hibernate is adding the child to the new parent first, then deleting it...
2. I told Hibernate to "delete orphans" but
within the context of the transaction the child is not an orphan since it has a new parent before the transaction is closed.
3. If point 2 above is irrelevant and "delete-orphan" semantics are to delete the orphan immediately when I remove it from the old parent, then how come it
doesn't delete if I add the child back to the same parent later in the transaction? (as in when I want to change the order of the children - see my last example in the orginal post)
4. Regardless, reparenting a child, even within the context of a "delete-orphan" cascading relationship, is a valid user operation and I think it should be allowed. This is something I could do in SQL (including the cascade delete) but can't do in Hibernate, and I was under the impression that we should be able to do everything in Hibernate :)
To be honest this is looking more and more like a bug. As far as I can tell it would be proper for Hibernate to either:
a. Delete the object immediately, and throw an exception if I try to add it to a parent again, even if it's the same parent
b. Only delete the orphan object after the transaction is closed and only if it hasn't been added to another parent.
With b. of course being the preferable one.
jits_1998 wrote:
In your case, you might be better of setting inverse='true' on the collection and then instead of adding and removing the group, just change parent in the group by group.setParent(newParent).
Would love to but this being an ordered collection (list) you can't use the parent relationship as the main one because you need to set the list index. Unless I find a solution I'll probably remove the "delete-orphan" cascade and delete orphans manually.