-->
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: Why unidirectional one2many w/o join table not recommended?
PostPosted: Tue May 06, 2008 8:25 am 
Newbie

Joined: Tue Jan 25, 2005 3:03 am
Posts: 11
Location: Bangalore, India
Hi,

The Hibernate Reference (Section 7.2.3) mentions that :
Quote:
A unidirectional one-to-many association on a foreign key is a very unusual case, and is not really recommended. We think it's better to use a join table for this kind of association.

Could any one please shed some light on why exactly it is not recommended? Why is it that the counter-argument, viz. that by skipping an intermediate association table we are avoiding an unnecessary join (and table), doesn't hold good?

Thx,
Vikas

_________________
Rgds,
Vikas.

"Quidquid latine dictum sit, altum sonatur".
Whatever is said in Latin, sounds profound


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 06, 2008 4:45 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
For one thing, it doesn't make much sense conceptually. The association is driven by the foreign key on your child entity, so that is really the managing side of the association. The simplest form is to have the parent unaware of the children, and each child simply aware of its parent through a many-to-one. Then when you need children, you query for them.

If you want to map the collection on the parent entity, you can add the <set> or whatever, and mark it inverse=true because the children still own/manage the association through the foreign key.

When you do a unidirectional one-to-many, the parent becomes the managing side of the association and this doesn't match up well with RDBMS relationships. You get things like extra updates -- when you insert a parent + children, you'll get inserts on each, then subsequent updates setting the parent ID on each child. It's just ugly.

So they say if you need a mapped collection towards children that are unaware of their parents, you use an association table because it makes more sense relationally and works a whole lot better.

But in your case, just use a bidirectional relationship :)

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 06, 2008 4:53 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
I should add that if you need a true parent/child relationship, the best way to get this is by using a collection of components, not an association mapping.

http://www.hibernate.org/hib_docs/v3/re ... ollections

This makes the children bounded by the lifecycle of the parent and works like you might expect. You cannot query for the children on their own, however.

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


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.