-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Cascade delete orphan and parents-childs relationship issue
PostPosted: Mon Oct 31, 2011 8:13 am 
Newbie

Joined: Mon Oct 31, 2011 7:38 am
Posts: 1
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!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.