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?
|