-->
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: Many To Many Link Table Problem
PostPosted: Fri Feb 17, 2006 6:31 am 
Beginner
Beginner

Joined: Mon Oct 17, 2005 1:58 am
Posts: 27
I have a two tables named student and exam.a many to many relationship is maintained between the two tables using a link table named studentexam.The table structure is as follows

STUDENT
- studentid
-name


EXAM
-examid
-examname
-year


STUDENTEXAM
-STUDENT_EXAM_ID
-STUDENT_ID
-EXAM_ID
-Grade
-totalmarks

Now i want to autopopulate the STUDENT_EXAM table
whenever some record is entered int the STUDENT and EXAM table

Is there any way in which i can do it?

Kindly help

Thanks...


Top
 Profile  
 
 Post subject: Are your sure about you DB design
PostPosted: Fri Feb 17, 2006 7:08 am 
Newbie

Joined: Wed Jan 11, 2006 8:24 am
Posts: 5
Fisrt I think you should check your databse design,
if you are looking for a Many-to-Many relation the join table should contain
only two keys STUDENT_ID and EXAM_ID.
STUDENT_EXAM_ID is not necessary since you apply the Unique constraints on both
STUDENT_ID and EXAM_ID.

I don't know wht means for you Grade and total marks so may be I'm wrong...

You need of course two collections on your entty object
Exams on Student class
Students on Exams class

Then the map files should look like these ones

For Student class

<set name="Exams" table="STUDENTEXAM" inverse="true">
<key column="STUDENT_ID"/>
<many-to-many class="Class Exam name,namespace name" column="EXAM_ID"/>
</set>

For Exam class

<set name="Students" table="STUDENTEXAM" cascade="all">
<key column="EXAM_ID"/>
<many-to-many class="Class Student name,namespace name" column="STUDENT_ID"/>
</set>

The token <set /set> is depending of your collection type.
the position of inverse and cascading is up to you depending which "direction" of the relation you want to use.

I suggest you to read the NHibernate doc concerning Many-to-Many and also Hibernate in action Chapter 6.3.2

Tips : It's better if the field name for your key is the same on evry table...


Hope this help you...


Top
 Profile  
 
 Post subject: Re: Are your sure about you DB design
PostPosted: Fri Feb 17, 2006 7:19 am 
Beginner
Beginner

Joined: Fri Nov 11, 2005 1:04 pm
Posts: 22
Bystronic wrote:
Fisrt I think you should check your databse design,
if you are looking for a Many-to-Many relation the join table should contain
only two keys STUDENT_ID and EXAM_ID.


Actually, I think you don't have a many to many situation at all. You should have a separate entity called student exams because each student has a grade for each exam they take (I think your totalmarks column is computed and should be part of the student table).

So you have a many-to-one from StudentExam to Student and a Many-to-one from StudentExam to Exam.


Top
 Profile  
 
 Post subject: That's true
PostPosted: Fri Feb 17, 2006 8:24 am 
Newbie

Joined: Wed Jan 11, 2006 8:24 am
Posts: 5
I think you don't have a many to many situation at all.

I'm agree STUDENTEXAM should correspond to an entity "Result" with two One-To-Many relations with Exam and Student.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 19, 2006 11:19 pm 
Beginner
Beginner

Joined: Mon Oct 17, 2005 1:58 am
Posts: 27
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.