-->
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.  [ 4 posts ] 
Author Message
 Post subject: Best practice for managing OneToMany relationships
PostPosted: Tue Aug 05, 2008 4:58 pm 
Newbie

Joined: Tue Aug 05, 2008 4:39 pm
Posts: 3
Hi!

I have some thoughts about how to to keep both sides of an OneToMany relationship in sync with each other. Are there any best practices or things to avoid in my example below?

Let's say I have classic Parent / Child case. One Parent can have many children, one Child can only have one Parent. So the Child is the owning side of the relationship.

Now if I create two instances like this (all in one transaction).

Parent p = new Parent();
persist(p);
Child c = new Child();
persist(c);
c.setParent(p);
print("Children: " + p.getChildren());

The print() statement will in this case print an empty set of children. If I do a refresh(p) before the print() then the child will show up in the set. And of course if I start a new transaction and load the parent from the databse the child will be there.

Now, how do I get the child to turn up in the parent side of the relation without doing refresh()? (Or should I at all try that?)

I could add some logic to the setters. E.g in the Child:

public void setParent(Parent parent) {
this.parent = parent;
parent.children.add(this);
}

Are there any dangers in doing something like this? Will I run into conflicts with Hibernate calling the setter?

Any advice would be helpful!

/Niklas


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 06, 2008 2:26 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
It is a good idea to make sure that both ends of the association are up-to-date. Having this code in a single method is also a good idea. However it may not be a good idea to put this code in in the setParent() method since, as you point, Hibernate is also calling the setParent() method. This will trigger all sorts of things (lazy loading of the parent and it's children collection for example) which you may not want to happen.

The Hibernate documentation has an example (http://www.hibernate.org/hib_docs/v3/re ... child.html)
where this is done from the parent side with methods that are not called by Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 08, 2008 8:21 am 
Newbie

Joined: Tue Aug 05, 2008 4:39 pm
Posts: 3
Ok, so the strategy is to keep bi-directional relations in sync, but not in methods that are called by Hibernate.

(I had read that chapter in the docs but missed that particular thing in the example code.. :-) )

Thanks for the advice!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 08, 2008 5:54 pm 
Newbie

Joined: Wed Dec 21, 2005 12:44 pm
Posts: 6
If a project is using field-based annotations instead of setter injection, then is there any reason that it would not be fine to actually put this relationship maintenance behavior in the setParent() method?


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