Given the following tree-like structure…
@Entity
class Node {
@Id
String id;
@ManyToOne
Node parent;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
Set<Node> children;
}
… I’d like to be able freely move children between parents. I can easily do that when all entities involved in such transaction (2 parents and 1 child) are already persistent. It’s impossible however to insert new (transient) node “between” two existing nodes.
I’m unable to make
become
if x is transient, as Hibernate deletes b.
Adding x in between a and b involves 2 steps:
- removing b from a.children
- adding b to x.children
I suppose that step 1. makes Hibernate to mark b for deletion. Step 2. should somehow undo this - and it does, but only when x is already managed (and so is x.children collection). It’s worth noting that x becomes managed in the very same transaction because of CASCADE.ALL on a.children. I even tried to explicitly call persist but it doesn't change anything - b is being deleted anyway. This looks like a bug.
I've created SSCCE:
Please let me know if I can help somehow. |