-->
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.  [ 3 posts ] 
Author Message
 Post subject: delete problem with bidirective many to many relationship
PostPosted: Thu Feb 19, 2004 1:52 am 
Beginner
Beginner

Joined: Mon Feb 09, 2004 6:27 am
Posts: 33
many to many bidirective relation
>>with parent class:
<set table="children" ....inverse="true" cascade="true">
<many-to-many.........

>>test code:
........
Parent3 pa=new Parent3();
pa.setName("pa");
Parent3 pb=new Parent3();
pb.setName("pb");
Child3 ca=new Child3();
ca.setName("ca");
Child3 cb=new Child3();
cb.setName("cb");
Set childset=new HashSet();
childset.add(ca);
childset.add(cb);
pa.setMychilds(childset);
pb.setMychilds(childset);
Set parentset=new HashSet();
parentset.add(pa);
parentset.add(pb);
ca.setMyparents(parentset);
cb.setMyparents(parentset);

Session sess1=sessions.openSession();
try{
tx=sess1.beginTransaction();
sess1.save(pa);
sess1.save(pb);
sess1.flush();
List pl=sess1.find("from Parent3 parent where parent.name=?",
"pa",net.sf.hibernate.Hibernate.STRING);
Parent3 p=(Parent3)pl.get(0);
sess1.delete(p);
tx.commit();
}catch(Exception e){
if(tx==null)tx.rollback();
throw e;
}finally{
sess1.close();
}

>>problem:
Exception in thread "main" net.sf.hibernate.ObjectDeletedException: deleted obje
ct would be re-saved by cascade (remove deleted object from associations): 2c9e8
1cdfacc46fa00facc46fd6e0002, of class: com.dnt.test.Child3
at net.sf.hibernate.impl.SessionImpl.forceFlush(SessionImpl.java:734)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:712)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1347)

how it happened like that?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2004 3:08 am 
Beginner
Beginner

Joined: Mon Feb 09, 2004 6:27 am
Posts: 33
sorry !! the cascade above is "all"

this time i change the cascade to "save-update"

everytime if i want to delete a parent p i hava to code:
Iterator it=parent.getChildren().iterator();
//for all children
Child c=(child)it.next();
c.getParents().remove(p);

it turn out to ok to run
but i figure that may cause some children became orphan.

if cascade is "all-delete-orphan" the problem as above happens again
i wonder how to configure and delete in the bidirective many to many situation??????????????


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2004 4:04 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
AFAIK all-delete-orphan can be achieved only with one-to-many

Implement your bidir delete manually

_________________
Emmanuel


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

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.