Hi,
I use Hibernate 3.3.2GA (supported JBoss.com version).
I have an issue when I try to relink objects in a parents-childs relationship.
Here is the Entity (note that the "parent" is omitted voluntarily):
Code:
public class EntityA{
@ManyToMany
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
private Collection<EntityA> childs = new ArrayList();
public java.util.Collection getChilds() {
return childs;
}
}
I try to insert an EntityA in the middle of the parents-childs structure (parent being the entry point):
Code:
hibernateTransaction.begin();
EntityA parent = (query to get the persistent parent)
EntityA newEntityA = new EntityA();
hibernateSession.save(newEntityA); // That probably could be omitted
Collection childs = new ArrayList(parent.getChilds());
newEntityA.getChilds().addAll(childs);
parent.addChild(newEntityA);
parent.getChilds().removeAll(childs);
hibernateTransaction.commit();
On the commit statement, I get:
Code:
org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [example.EntityA#45]
I guess I understand what is going on: the “parent.getChilds().removeAll(childs);” triggers the delete cascade, but since those same entities are being linked to another object, I would have expected the delete cascade not to be enforced.
Anyone has a way around this? I would prefer not to modify EntityA, but I am open minded.
Thank you!