-->
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 where middle table has extra fields
PostPosted: Sun Jan 23, 2011 4:21 am 
Newbie

Joined: Sun Jan 23, 2011 4:14 am
Posts: 7
Hi,

What kind of mapping will we have if we have additional fields in middle table, other than the foreign keys connecting the 2 tables?

Existing:

Student(key:studentid, column:name)
Student_courses (foreign keys studentid and courseid) <---middle table
Courses(key:courseid, column:description)

New:

Student(key:studentid, column:name)
Student_courses (foreign keys studentid and courseid, ADDITIONAL FIELDS: 2 NON-KEY FIELDS: STARTDATE AND ENDDATE) <---middle table
Courses(key:courseid, column:description)

As seen above, i added startdate and enddate, 2 strings.

Existing hibernate will be like this:

Code:
<hibernate-mapping> 
    <class name="aaa.Student" table="STUDENT">   
        <id name="studentId" type="long" column="STUDENTID"> 
            <generator class="assigned" /> 
        </id> 
        <property name="name" type="string" length="200" column="STUDENTNAME" /> 
        <set name="courses" table="STUDENTCOURSE" cascade="all"> 
            <key column="STUDENTID" /> 
            <many-to-many column="COURSEID"  class="aaa.Course" /> 
        </set> 
    </class> 
</hibernate-mapping>




and

Code:
<hibernate-mapping>
<class name="aaa.Course" table="COURSE">
    <id name="courseId" type="long" column="COURSEID">
        <generator class="assigned"/>
    </id>
    <property name="courseName" type="string" column="COURSEDESCRIPTION"/>
</class>
</hibernate-mapping>




Will you let me know how the new one will be?


Top
 Profile  
 
 Post subject: Re: many-to-many where middle table has extra fields
PostPosted: Sun Jan 23, 2011 7:49 am 
Newbie

Joined: Mon Jan 10, 2011 7:21 pm
Posts: 8
you can achieve this type of mapping by creating a new Entity called StudentCourse which encapsulates the fields you need and map a one-to-many relationship from Student and a many-to-one from StudentCourse to Course as in:
Student -->(one-to-many) -->StudentCourse --> (many-to-one) --> Course

Regards,
-- Savvas Andreas


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.