-->
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.  [ 2 posts ] 
Author Message
 Post subject: Many-to-many and ternary association best practice?
PostPosted: Sat May 15, 2004 5:42 pm 
Newbie

Joined: Fri Mar 05, 2004 10:41 am
Posts: 12
I'm a Hibernate newbie, so I apologize in advance if my questions sound stupid :-). I really want to get a good handle on the best approach with respect to database performance, i.e., never retrieve more records that I need, and always try to make the fewest database calls.

As we all know, in the relational model, a many-to-many or ternary relationship is represented as a link table. My question is: should this link table be modeled as a top level entity in my domain model? My typical use cases are:

1. in a many-to-many association, with tables A, B, and L, I always want to find the list of B given an A

2. in a ternary association, with tables A, B, C, and L, I always want to find the list of C given an A and a B

For my use case, it seems that modeling the link table as a top level entity always seem to make sense, as it gives me exact control how many records/object instances I retrieve from the database.

Now another follow up question:

In my LClass that maps to the link table, should I have many-to-one references to AClass, BClass, and CClass, or should I just have regular attributes for a_id, b_id, and c_id, if all I want to do is to update the associations in the link table?


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 16, 2004 4:39 pm 
Newbie

Joined: Fri Mar 05, 2004 10:41 am
Posts: 12
Here is what I found out so far:

MANY-TO-MANY

    1. If the 2 foreign keys form a unique key in the link table, then there is no need to have a top lebel LClass, even if there are extra columns in the link table. Take a look at the Order-Product relationship in Chapter 18 of the reference doc.
    2. If the 2 foreign keys DO NOT form a unique key in the link table, then you need a top level LClass that maps to the link table. The Employer-Employee example in the same chapter as above shows this.

TERNARY ASSOCIATION
    1. If projection is along one dimension, i.e., query against one participant of the relationship, then there is no need to have a top level LClass.
    2. If query is against two participants in the relationship, or there are extra attribute(s) in the link table, it is much easier to have a top level LClass that maps to that table. As this point I have not seen a complete sample for ternary relationship anywhere.


Let me know if I missed the boat.


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