-->
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.  [ 8 posts ] 
Author Message
 Post subject: Many to Many with state
PostPosted: Thu Jul 08, 2010 8:56 pm 
Newbie

Joined: Thu Jul 08, 2010 8:45 pm
Posts: 3
Take a simple example. Student and Course. Many to many relationship.
Student (id, name, ssn, ...)
Course (id, name, ...)

Using hibernate many-to-many does not allow me to store state. So if the join table was StudentCourse(studentid, courseid, dateEnrolled). How can I use the hibernate many-to-many to specify dateEnrolled? From which side(student or course) can i specify state? or do i have to deconstruct the many-to-many relationships into two one-to-many relationship by creating a new object StudentCourse in my domain model?

Can someone please explain how state is stored in many to many relationships?

Thanks.


Top
 Profile  
 
 Post subject: Re: Many to Many with state
PostPosted: Sat Jul 10, 2010 7:35 pm 
Newbie

Joined: Sat Jul 10, 2010 4:22 pm
Posts: 3
dehydratedpaani wrote:
Take a simple example. Student and Course. Many to many relationship.
Student (id, name, ssn, ...)
Course (id, name, ...)

Using hibernate many-to-many does not allow me to store state. So if the join table was StudentCourse(studentid, courseid, dateEnrolled). How can I use the hibernate many-to-many to specify dateEnrolled? From which side(student or course) can i specify state? or do i have to deconstruct the many-to-many relationships into two one-to-many relationship by creating a new object StudentCourse in my domain model?

Can someone please explain how state is stored in many to many relationships?

Thanks.


I'm trying to do the same thing. From my investigation, it seems in this case you do not want Hibernate to automatically generate the StudentCourse intermediary table. It is possible I am incorrect, but I've been searching for a solution to the same question you've posed for the past 3 hours. I think the latter approach you mentioned is correct. You'll have to manually create a StudentCourse class and then do two One to Many mappings.


Top
 Profile  
 
 Post subject: Re: Many to Many with state
PostPosted: Sat Jul 10, 2010 8:39 pm 
Newbie

Joined: Sat Jul 10, 2010 4:22 pm
Posts: 3
I can't get this to work using two One to Many relationships to the intermediary table. What would the pk of the intermediary table be? I get a Hibernate error saying the object for the table is missing the pk.


Top
 Profile  
 
 Post subject: Re: Many to Many with state
PostPosted: Sun Jul 11, 2010 8:56 am 
Newbie

Joined: Tue Jun 29, 2010 2:14 am
Posts: 8
siddiquiha wrote:
I can't get this to work using two One to Many relationships to the intermediary table. What would the pk of the intermediary table be? I get a Hibernate error saying the object for the table is missing the pk.


That's because each hibernate entity has to have a PK
Create an id column in the third object


Top
 Profile  
 
 Post subject: Re: Many to Many with state
PostPosted: Mon Jul 12, 2010 8:31 am 
Newbie

Joined: Mon Nov 30, 2009 2:19 pm
Posts: 7
you can also use an combined PK for the relation entity. just use both id's for the primary key, so you do not need to introduce an artificial PK for the relation table. this will also prevent double booking of an student to the same course ;)

greets


Top
 Profile  
 
 Post subject: Re: Many to Many with state
PostPosted: Mon Jul 12, 2010 9:07 am 
Newbie

Joined: Thu Jul 08, 2010 8:45 pm
Posts: 3
So can any experienced hibernate gurus enlighten us on the problems they faced when manually managing a many-to-many relationship as two one-to-many relationships? In terms of search criteria, caching, or other areas? I dont like this, to have to manually manage the mapping.


Top
 Profile  
 
 Post subject: Re: Many to Many with state
PostPosted: Tue Jul 20, 2010 9:28 am 
Beginner
Beginner

Joined: Thu Jun 21, 2007 9:24 pm
Posts: 20
Location: Lansing, Michigan, USA
dehydratedpaani wrote:
So can any experienced hibernate gurus enlighten us on the problems they faced when manually managing a many-to-many relationship as two one-to-many relationships? In terms of search criteria, caching, or other areas? I dont like this, to have to manually manage the mapping.


I had experience with one system where the many-to-many relationship, mapped as an in-between child entity, was actually a one-to-many relationship; furthermore, there was some glitch in the application that caused spurious extra rows (that is, child entities) to be created. For our report queries, we ended up including a subselect:

Code:
  ...
  LEFT OUTER JOIN (SELECT one_id,many_id FROM relationship_table
                   GROUP BY one_id,many_id) rel ON one_table.id=rel.one_id
  LEFT OUTER JOIN many_table ON rel.many_id=many.id
  ...


There are other options besides a child entity and one or two <set> mappings:

* <map> with a component as the key

* use a where=" ... " attribute on the mapping. So say the student_course table is defined as

Code:
  student_id INT,
  course_id INT,
  PRIMARY KEY(student_id,course_id),
  status CHAR(9) not null default 'ENROLLED',
  check (status IN ('ENROLLED','WITHDRAWN'))


you should be able to add a mapping

Code:
  <set name="enrolledCourses" table="student_course" where="status='ENROLLED'" >
        <key column="student_id"/>
        <many-to-many column="course_id"
            unique="true"
            class="Course"/>
    </set>


Top
 Profile  
 
 Post subject: Re: Many to Many with state
PostPosted: Tue Jul 20, 2010 3:49 pm 
Newbie

Joined: Sun Jul 18, 2010 10:15 am
Posts: 8
vipavel wrote:
That's because each hibernate entity has to have a PK
Create an id column in the third object

true, but have you thought about a composite element with a nested many-to-one relationship to student?
for example three classes
Course
Enrollment(no id) with dateEnrolled field
Student

where Enrollment is a composite element of Course with a many-to-one relationship to Student.

take a look at the reference 8.2
http://docs.jboss.org/hibernate/stable/ ... nents.html

is this what you want to do?


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