-->
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: Bidirectional associations question
PostPosted: Wed Jan 11, 2006 9:06 am 
Beginner
Beginner

Joined: Fri Sep 24, 2004 7:15 am
Posts: 40
Hi,

In my application I have 2 classes which have a typical one-to-many bidirectional association. The mapping is also the typical one, where the parent has a set with inverse="true" and cascade="save-update", and the childs have a many-to-one with cascade="none"

Then different screens are given to the user so he can add/edit/delete parents or childs. I mean, there is a screen where the parents can be added/edited/deleted and where their childs can be only seen.

Then there is another screen where the childs can be added/edited/deleted, choosing a parent from a combo, so when the object is being persisted, the child's parent variable is correctly informed.

But when I save a child, and go back to the parents screen, I show the parent's childs and the recently created child does not appear.

Does this mean that I always have to save the child from the parent object? Does it have to be with the fact that, when creating the child, the parents listed in the combo box are instances different from the parent that is loaded afterwards? Shouldn't it be the same parent, as they have the same ID and should be taken from the second level cache?

The fact is that now if I create or delete a child, and then I immediately go to the parent screen to show its childs, they do not appear at all (until the cache expires and the parent is loaded again from the BD).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 11, 2006 9:20 am 
Newbie

Joined: Thu Jan 05, 2006 7:33 pm
Posts: 13
hi e

best if you post your mapping files and some sample code here to help. Are you getting any errors? The stack trace might help.

Its not clear from your story if :
- the PARENT objects in the combo box have been persisted or not
- you are calling any Session methods
- getting any Exceptions
- the DB content is changing at all

Quote:
But when I save a child, and go back to the parents screen, I show the parent's childs and the recently created child does not appear.

Does this mean that I always have to save the child from the parent object?


It seems as though you have set cascade="none", on the CHILD. This means when you save a CHILD, hibernate does not force a save onto the parent. If you set cascade="all" etc, the save will carry to the PARENT object. However if the PARENT is already persisted when you call save after creating an association with the CHILD then whether cascade is set to all or none should not matter since the important state is the foreign key in the CHILD objects table. Again this depends on many factors...Post some code.

Michael


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 11, 2006 11:10 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Sounds like you might not be managing the bi-directionality of the association. Meaning, "adding a child to a parent" really implies two operations:
Code:
child.setParent( parent );
parent.getChildren().add( child );


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.