Hi,
I have a scenario where I want to delete set of Child Records and then insert a new set of Child Records and association details in the hbm files are as follows
Parent
<set name="childs" inverse="true" cascade="all">
<key>
<column name="PARENT_ID" precision="9" scale="0" not-null
="true" />
</key>
<one-to-many class="com.test.Child" />
</set>
Child
<many-to-one name="parent" class="com.test.Parent" >
<column name="PARENT_ID" precision="9" scale="0" not-null="true" />
</many-to-one>
Code to delete and add child objects
Code:
Parent parent = (Parent)session.load(Parent.class, new Long(id));
Iterator iterator = (Iterator) parent.getChilds().iterator();
while(iterator.hasNext())
{
Child c = (Child) parent.getChilds().iterator().next();
parent.getChilds().remove(c);
session.delete(c);
session.flush();
}
for (int i = 0; i < newChildsList.size(); i++) {
Child c = (Child) newChildsList.get(i);
c.setParent(parent);
set.add(c);
}
parent.setChilds(set);
session.saveOrUpdate(parent);
transactio.commit
It didn't worked no matter what I set for cascade attribute. But the interesting thing is that the following code works ( deletes only one child record and inserts new set )
Parent parent = (Parent)session.load(Parent.class, new Long(5));
Child c = (Child) parent.getChilds().iterator().next();
parent.getChilds().remove(c);
session.delete(c);
session.flush();
HashSet set = new HashSet();
for (int i = 0; i < ll.size(); i++) {
Child ch = (Child) ll.get(i);
ch.setParent(parent);
set.add(ch);
}
parent.setChilds(set);
session.saveOrUpdate(parent);
Code:
I dont know where I am doing wrong. Can any one help me in resolving the above.
Thanks
Prabhu