-->
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: one-to-many with join table vs without join table mapping
PostPosted: Mon Feb 22, 2010 5:22 am 
Newbie

Joined: Mon Jan 25, 2010 3:49 am
Posts: 3
Hi community,
I found the statement in hibernate documentation chapter 7.2.3 that states:
Quote:
A unidirectional one-to-many association on a foreign key is an unusual case, and is not recommended.


http://docs.jboss.org/hibernate/stable/ ... tions.html
and it recommend that to use the 'unidirectional one-to-many association on a join table'

Look like mapping the one-to-many relationship by using the foreign key is about the same as using the join table, what are the other impact or pro and con in using one-to-many mapping by foreign key?

I 'm newbie to hibernate so hope anyone of you can point me out. thanks


Top
 Profile  
 
 Post subject: Re: one-to-many with join table vs without join table mapping
PostPosted: Mon Feb 22, 2010 7:33 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Unidirectional(!) one-to-many association without join table is in my opinion not recommended,
because it breaks UML-specification:

Code:
+-------+   1               *   +---------+
|   A    | ---------------------> |    B    |
+-------+                        +---------+


as the association is unidirectional, 'b' should not know anything about 'a',
only 'a' should know about associated 'b' entities.
Therefore it is not correct to save the association information physically into b (as foreign key).
Also consider optimistic locking with corresponding versioning:
if you associate 'a' with 'b' then you would expect a version increment on 'a' only.
Without join table you have indeed a physically change and therefore a version increment on 'b' too.


Top
 Profile  
 
 Post subject: Re: one-to-many with join table vs without join table mapping
PostPosted: Mon Feb 22, 2010 9:53 pm 
Newbie

Joined: Mon Jan 25, 2010 3:49 am
Posts: 3
Hi pb00067,
thanks for your quick reply.

So using the foreign key in one-to-many association will not only pollute the class B with unnecessary foreign key, but it also affect the locking version?

The join table also come in with the trade off of the extra table in db and more complex in the configuration. Is the performance will be affected by using the join table way of association?


Top
 Profile  
 
 Post subject: Re: one-to-many with join table vs without join table mapping
PostPosted: Thu Feb 25, 2010 9:50 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
So using the foreign key in one-to-many association will not only pollute the class B with unnecessary foreign key

the foreign key is not unnecessary In this scenario, as it is the only field to hold the relation information.

Quote:
it also affect the locking version?


Yes, see here
Example association a with b :
Code:
with join-table:
update      A     set        version=1     where        id=1         and version=0
insert     into        A_B        (A_id, B_id)     values        (1, 1)


Code:
with foreign key:
update   A     set        version=1   where        id=1         and version=0
update   B     set        assA_id=1,  version=1     where        id=1        and version=0


As you can see the association with foreign key also increments implicitly the version of b from 0 to 1.

Quote:
The join table also come in with the trade off of the extra table in db and more complex in the configuration.


Yes, the schema on database gets a little bit more complicate.

Quote:
Is the performance will be affected by using the join table way of association?


It depends on the database you are using and if the join-table has the proper indexes working
(primary key (A_id, B_id) and unique (B_id) in our case)
Modern database should no have performance problems for executing simple join-queries,
so generally you should take a decision based on your needs for locking and concurrency.


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.