-->
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.  [ 2 posts ] 
Author Message
 Post subject: Deleting Child records in One to Many Association
PostPosted: Fri Jan 04, 2008 4:48 pm 
Newbie

Joined: Fri Jan 04, 2008 10:42 am
Posts: 1
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


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 05, 2008 1:27 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Prabhu,


Your code would require some changes

Code:
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();
}



should have been

Code:
Iterator iterator = parent.getChilds().iterator();
while(iterator.hasNext()){
    Child c = (Child) iterator.next();
    iterator.remove();
    session.delete(c);
}
session.flush();


Differences:
- you are acquiring an iterator on each loop. That is not necessary.
- you are acquiring a reference to the list each time. That is not necessary. You just need to delete elements from the list to make Hibernate know that the relationship between Parent and Child is to be destroyed.
- you don't need to flush the session all the time. Let hibernate decide when is convenient to execute a batch of SQL

It is also considered a good practice to create "addChild(Child c)" methods, that add the child to the inner collection and set the parent property to the child, all in the same method.


Also notice, your first piece of code aims to delete both the children themselves and their association with the parent, while the second code snippet, the one you say worked, deleted just the association, not the child objects themselves. Make sure you understand the difference between both concepts.


Finally, in all this explanation I am assuming there was no session/transaction issue (I see a transaction.commit somewhere, but nothing else).

_________________
Gonzalo Díaz


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.