-->
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: Many-to-Many relationship sanity check
PostPosted: Thu Oct 16, 2003 2:19 pm 
Beginner
Beginner

Joined: Mon Sep 29, 2003 3:10 pm
Posts: 36
Ok, I think I'm handling this right, but I'm still so new to Hibernate that I wanted to run this by anyone with a bit more experience to make sure I'm not off base.

I have two entities in my system, a CBTCourse and a User. Theses two entities have a many-to-many relationship. That relationship is actually defined in its own class, AssignedCourse because the relationship itself has some properties. For instance, a couple of those properties are targetCompleteionDate, assignedDate, and proficiencyLevel.

So at this point in my system, there are the three entities, CBTCourse, User, and AssignedCourse. Because the relationship between CBTCourse and User is a many-to-many one, my first instinct was to place the following in the User mapping file:
Code:
<set
    name="courses"
    lazy="true"
    inverse="false">

    <key column="user_id" />

    <many-to-many
    class="com.cadtrain.coachlms.common.dataobject.CBTCourseDO"
    column="course_id"
    outer-join="auto" />
</set>

However, I couldn't figure out how to continue down this path and capture the properties that I knew were represented by the AssignedCourse class.

So now, I'm approaching this differently and it's this alternative approach that I wanted to run past some folks with a bit more experience with Hibernate than myself. I'm relating the User to the AssignedCourse with a one-to-many mapping and then relating the AssignedCourse to the CBTCourse with a many-to-one mapping.

Does this make sense? It appears to do so, from my perspective, but for some reason, I feel like I'm violating some kind of sacred rule somewhere along the way. :)

One of the by-products of this approach was that the associative entity, the AsignedCourse is actually using a surrogate key instead of a composite key made up of the course_id and the user_id. Again, this feels quiet odd from the perspective the the database relational model, but from reading the Hibernate refernce manual, there appeard to be quite a few pitfalls associated with composite keys that I really didn't want to mess with.

-Matt


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2003 10:46 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
I'm relating the User to the AssignedCourse with a one-to-many mapping and then relating the AssignedCourse to the CBTCourse with a many-to-one mapping


Yep, this is called manual decomposition of a many-to-many association, where the many-to-many is broken up into 2 one-to-manys. So your not violating any sacred rules here :) In fact I use this quite a bit myself.

As for your quesiness about the surrogate key on the association entity, my take on that (at least where I controll the DB schema) is that it is now a top level entity and I want all my top level entities to have surrogate keys.

HTH


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2003 11:23 am 
Beginner
Beginner

Joined: Mon Sep 29, 2003 3:10 pm
Posts: 36
Thanks for the reply!

-Matt


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.