-->
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.  [ 5 posts ] 
Author Message
 Post subject: bi-directional many-to-many with associative data
PostPosted: Fri Oct 15, 2004 11:11 pm 
Newbie

Joined: Tue Sep 07, 2004 12:00 am
Posts: 6
i've been weeding through the documentation and the forums for a few weeks now and it remains unclear to me how best to accomplish this.

take the classic, STUDENT, COURSE, STUDENT_COURSE scenario where
STUDENT_COURSE holds a GRADE column.

so we want to be able to accomplish the following tasks:

(1) given a STUDENT, obtain a collection of COURSEs that they have taken

(2) given a COURSE, obtain a collection of STUDENTs that have taken it

(3) given a STUDENT and a COURSE, obtain the associated GRADE

(4) perform queries such as:

(4.1) get me a list of STUDENTs that have gotten a 'B' in the 'math' COURSE

(4.2) get me a list of COURSEs that have been taken by STUDENTS named 'Fred'

From what i can gather, there are (at least) three approaches, and here are some pros and cons that i can see, please correct me if i'm wrong w.r.t. if they are appropriate approaches or pros/cons:

(A) two one-to-many's

pro:
- easiest to map and understand

con:
- many-to-many isn't explicit such that traversal is more tedious

notes:
- not too concerned with "tedium" as long as items (1-4) above can be accomplished with same performance as (B) and (C) below

- can someone provide syntax examples (or links to) for (1-4) above?

(B) composite-element

pro:
- as opposed to (A), would provide explicit support for traversal of many-to-many

con:
- would have to create redundant STUDENT_COURSE definitions on both STUDENT and COURSE mapping

(C) index-many-to-many

pro:
- as opposed to (A), would provide explicit support for traversal of many-to-many

- as opposed to (B), would allow for single definition of STUDENT_COURSE entity

--------------------------------------------------------------
it seems that any non-trivial application would come across the need for an "associative entity" holding data, perhaps bi-directional traversal isn't strictly required, but i think all would benefit from an example similar to above with hbm files and code samples added perhaps to section 18 of the documentation.


Top
 Profile  
 
 Post subject: I have exactly the same situation
PostPosted: Mon Oct 18, 2004 12:44 pm 
Newbie

Joined: Thu Oct 14, 2004 12:30 pm
Posts: 3
I am trying to solve exacty the same problem and have also not found much about the details of how to do this. I think that a good example (including Java code) of this situation would be very valuable for many developers. If anyone can point to an example of breaking a many-to-many into two one-to-manys w/ an association entity, it would be greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 18, 2004 2:02 pm 
Newbie

Joined: Fri Aug 27, 2004 5:36 pm
Posts: 16
Location: Pittsburgh
I had the similar issue but i never to use to one-to-many in place of many-to-many because one and only reason, the many-to-many relationship table had few more coulumns besides the compiste elements.

I had to create a separate class, in your example it would be StudentCourse.

StudentCourse {

Student s ;
Course c;
String grade;

// and other attributes.
}

_________________
--------------------------------------------
Vish
Help others too


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 18, 2004 2:11 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
tell me if this help
http://forum.hibernate.org/viewtopic.php?t=933543

be carefull of which collection you use on each side (see reference guide or hibernate in action)

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Tue Oct 19, 2004 10:25 am 
Newbie

Joined: Thu Oct 14, 2004 12:30 pm
Posts: 3
Yes, this link was very helpful. I still don't have it working quite right, but I think it's because of the way I'm handling the collections within my Java code and also getting equals() and hashCode() correct.

Thanks.


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