-->
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.  [ 6 posts ] 
Author Message
 Post subject: update of parent/child semantics problem. example code Pls.
PostPosted: Fri Feb 20, 2004 4:52 pm 
Newbie

Joined: Thu Feb 19, 2004 11:35 pm
Posts: 8
Hi,

I ran across a problem and tried different solutions without any luck. I dont think there's a bug in hibernate but probably a lack of similar kind of example and my understanding of hibernate, I am not able to sovle it yet.

So, now I am trying to figure out how other people are solving this problem as I BELIEVE this is a common use-case.

1. User loads a parent object with children collection in a session.
2. User updates the parent object and adds/removes/updates some children and pass them on for update.

* Due to the disconnected nature of the application, we dont keep the children ids as these are meaningless for the user (and GUI).
* We want to avoid finding out new/modified/delete children and hence want to throw away existing children and save new ones and update the parent object.

I've gone through all FAQs and hibernate javadocs without any luck.

Can somebody please guide me to an example of above update use-case.

Thanks,
Tahir


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 5:31 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
http://www.hibernate.org/hib_docs/reference/html_single/#parent-child


Top
 Profile  
 
 Post subject: Re:
PostPosted: Fri Feb 20, 2004 8:23 pm 
Newbie

Joined: Thu Feb 19, 2004 11:35 pm
Posts: 8
I mistyped when I wrote javadocs.......actually I've printed and read the complete manual (PDF) and could not find an example of one-shot-delete in my scenario.

I tried various solutions but nothing has worked so far. Should I start posting exceptions/warnings in each of those cases?

Tahir


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 8:31 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
It looks like you are a bit confused. one shot delete does not apply to parent/child relationships. It is mainly of value for many-to-many associations and collections of values.

Dereferencing a collection of entities does NOT delete them. This should be quite clear from the documentation.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 21, 2004 1:09 am 
Newbie

Joined: Thu Feb 19, 2004 11:35 pm
Posts: 8
Gavin: Thanks for your reply. Can you point me to some open-source example to learn using parent/child with hibernate.


No doubt, I am confused. I "now" have the following line working that removes the existing children from the DB during the update.
voyage.getVoyagePorts().clear();


For Add-New, I had code that used to work with hibernate 1.x and stopped working in 2.x till I changed to following.

try {
tx = session.beginTransaction();
session.save(voyage);
session.flush();

// previously only voyage.setVoyageCrew(crewData); was required and the children collection was saved. But now, I have to do the following.

for (int i=0; i<crewData.size(); i++) {
VoyageCrew vc = (VoyageCrew) crewData.get(i);
vc.setVoyage(voyage);
session.save(vc);
}
tx.commit();
} catch (HibernateException e) {
if (null != tx)
tx.rollback();
log.error("Error in addVoyage",e);
throw e;
} finally {
session.close();
}

Now, I am fine as my code is working now but it would be nice to know exactly how Hibernate expect me to write addNew and update methods.

Tahir


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2004 2:48 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
tsawan wrote:
Gavin: Thanks for your reply. Can you point me to some open-source example to learn using parent/child with hibernate.


http://www.hibernate.org/159.html
http://www.hibernate.org/78.html

You'll probably find what you want.

_________________
Emmanuel


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