-->
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: Indirectly persisting the state of associated objects
PostPosted: Fri Nov 03, 2006 8:34 pm 
Newbie

Joined: Wed Feb 08, 2006 8:29 am
Posts: 10
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

3.1.2

[Oracle 9.2

Hi all,

This is more of a general question. Its probably pretty obvious, and I'm totally missing the point, but I searched the forum for similar stuff and didn't find anything, which probably means I'm really bad at searching ;)

I'm wondering if it is correct to do this. Say you have a main object, that is joined to linked objects, one-many. I.e. the linked objects hold the key to the main obj. Lets also say adding these links can be built up over subsequent session interactions, in the same or different session.

You update the linked objs by adding and deleting from the set, bag and list objects in the main object, in java-land, by adding/deleting to the sets, etc.

My question is, do you need to save off the individual objects separately to the main object, or can you cause the linked objects to be created/updated/deleted by manipulating the collections of these objects in the main object and merely saving the main object, by exploiting the cascade attribute?

Its just that I've been trying to do this, but I always run into difficulties, which I can usually solve by saving the linked object and its link after saving the main object, with the links removed first. And I have to mess around with the cascade and inverse attributes on the set/bag/list nodes. But I get it working, by writing ugly code to compensate.

Usually the main issue I encounter is where it is trying to insert the linked records before the main one. and as they hold the main object link, thats null, and then it inserts the main object, and the linked object main ref is null. This is because I've done stuff like
main = new Main();
link.setMain(main);
main.getLinks().add(link);
main.save();

So it createds link, then creates main, and link.mainid is null in the database.

Am I misusing hibernate trying to do it this way? The docu does not indicate that I can do this directly. Or is this the beauty of hibernate that you can do this, and I've totally missed it?

Apologies if this has been covered before, it must have been as its fundamental to how you use it. If I've got it wrong, thats a lot of code I'll have to rewrite :)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 04, 2006 5:06 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
When you mention the inverse property on the one-to-many side of things, Hibernate will only do the update if the mapping is done on the other (inverse) side. Read about the inverse setting in the Hibernate documentaiton. That in combination with the cascade attribute should get to what you need. You shouldnt have to separately persist the link objects, just having a cascade and persisting(save or update) the main object should be good enough.
If you have specific problems, please post the situation and the stacktrace.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 06, 2006 5:09 am 
Newbie

Joined: Wed Feb 08, 2006 8:29 am
Posts: 10
Thank you Rajasaur, that helps alot to know I'm not doing something I shouldn't be. I need to spend some time trying to see what I am doing wrong. I will scrutinise the inverse docu more closely.

juo


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.