-->
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.  [ 5 posts ] 
Author Message
 Post subject: can't get update to work with collection
PostPosted: Mon Sep 01, 2003 12:40 am 
Newbie

Joined: Mon Sep 01, 2003 12:31 am
Posts: 5
Hi,

I did read the chapter on parent/child relationship but could not find much help.

I have a parent/child relationship with a <bag> and inverse=true.

I can add a parent with children like below.
session.save(parent);
parent.setParent(); // set parent for all children objects
session.update(parent);

Problem is that when I update the parent, the children are duplicated (in other words, old children are not removed).

My GUI has a table/grid where user can add/update/delete children. So, I thought I could just use parent.setChildren(List children) and hibernate will take care of removing the old children but that didn't work.
I also tried by explicitly removing the children as

parent.setChildren(new Vector()); // to clear old children
session.save(parent);
parent.setChildren(updatedChildren);
session.save(parent);

but the problem is still there. I AM using cascade='all'. I just can't get rid of old children with this code. Hopefully I dont have to iterate over old children and call session.delete on all children.

Please let me know if I am doing anything wrong here.

_________________
Thanks,
Tahir


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 01, 2003 3:33 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Is you unsaved-value mapping correct?


Top
 Profile  
 
 Post subject: update semantics
PostPosted: Mon Sep 01, 2003 9:44 am 
Newbie

Joined: Mon Sep 01, 2003 12:31 am
Posts: 5
Yes, I have unsaved-value="0" in my child table's mapping.

I guess the problem is that I am not very sure how should I perform an update. Options are

1. Not worrying about existing data and just delete the old children and add the new ones. As the frequency of update is very low in my case, this is a very acceptable solution. I also dont have to worry about preserving the id (pk) in this case.

2. Optimized update. Just updating (deleting) what user has really changed. In this case, I have to store the ids in gui table so an update can be performed later (based on the ids). I am not too sure if I can create new instances with the old id before update (very easy to do)? Or do I really have to use the original instances during update. Ideally I would like to do this in a very disconnected manner where I just create a new collection and use existing ids from original collection.

Are there any design patterns or example code that does the above?? I guess a junit test case is easier to write as it runs in a single session. But having a client gui updating existing collection is little different.

_________________
Thanks,
Tahir


Top
 Profile  
 
 Post subject: its fixed now.
PostPosted: Mon Sep 01, 2003 12:14 pm 
Newbie

Joined: Mon Sep 01, 2003 12:31 am
Posts: 5
Hi,

Again, it was my misunderstanding of cascade='all-delete-orphan'. I thought it will only work if I remove the parent link from the children. But as it turns out, it worked for me to have one-shot delete.

So, now I am clearing the exisitng collection by
parent.getChildren().clear();
session.flush();
parent.setChildren(newChildren);
session.update(parent);
session.flush();

and its working like a charm.

Pls. let me know if its the right way of doing things.

_________________
Thanks,
Tahir


Top
 Profile  
 
 Post subject: Answer
PostPosted: Thu Sep 25, 2003 1:36 pm 
Beginner
Beginner

Joined: Wed Aug 27, 2003 6:44 pm
Posts: 20
>>cascade='all-delete-orphan'
All-delete-orphan means when object is removed from the collection remove it.


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