-->
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.  [ 3 posts ] 
Author Message
 Post subject: saving newly added objects when updating containing object
PostPosted: Wed Jul 30, 2008 5:42 pm 
Newbie

Joined: Wed Jul 30, 2008 5:30 pm
Posts: 2
Can someone here help me with the following situation?

* I'm updating a detached object (an Entity)
* some of the properties which were previously null (such as Entity.email, of type EmailAddress) have now been set to new, unsaved objects

Is there some way to get Hibernate to automatically insert the new EmailAddress, even though I am updating the Entity? I've set the email property to cascade all, but that doesn't seem to work. So I changed my Entity DAO to detect that the EmailAddress is unsaved and save it manually.

This works, but I'm wondering whether it is the best way to do it. Also, it introduces a problem: if the transaction is rolled back, the ID is nevertheless still set on the unsaved EmailAddress, so if I try again to update the containing Entity I get a constraint violation. Is there an accepted way of avoiding this problem?

Kind regards,
Pepijn Schmitz


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 31, 2008 4:25 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
If the unsaved objects are associated with the detatched entity, simply saving the associated objects is enough - they'll get inserted into the database with the appropriate linking ID.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 31, 2008 4:56 pm 
Newbie

Joined: Wed Jul 30, 2008 5:30 pm
Posts: 2
Thanks. I guess you're saying that there's no way of having Hibernate do that automatically when I update the top-level object?

Do you know of some way of avoiding the problem that when I save those new objects their ID gets set, so that if the transaction is rolled back and I try to save them again Hibernate thinks they're already saved because the ID is already set?


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