-->
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: Mamy to many relationship with 3 tables
PostPosted: Tue Dec 02, 2003 9:45 am 
Newbie

Joined: Tue Dec 02, 2003 9:12 am
Posts: 7
Hi,

I am trying to make a many to many relationship that involves 3 tables, 'Student', 'Faculty' and 'course'. Here the many to many relationship is between faculty and student. Mapping table is 'course'. This is how I have the mapping

<class name="com.nielsenmedia.manytomany.entities.Course" table="course" dynamic-update="true" dynamic-insert="true" >
<composite-id>
<key-many-to-one class="com.nielsenmedia.manytomany.entities.Student"
name="student" column="STUDENT_ID"/>
<key-many-to-one class="com.nielsenmedia.manytomany.entities.Faculty" name="faculty"
column="FACULTY_ID"/>
</composite-id>
</class>

<class name="com.nielsenmedia.manytomany.entities.Faculty" table="faculty" dynamic-update="true" dynamic-insert="true" > <id name="facultyID" type="java.lang.Long" column="FACULTY_ID" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="facultyName">
<column name="FACULTY_NAME" sql-type="varchar(20)"/>
</property>
<set name="students" table="course" inverse="true" cascade="save-update">
<key column="FK_FACULTY_ID"/>
<many-to-many class="com.nielsenmedia.manytomany.entities.Student"
column="FK_STUDENT_ID"/>
</set>
</class>

<class name="com.nielsenmedia.manytomany.entities.Student" table="student" dynamic-update="true" dynamic-insert="true" >
<id name="studentID" type="java.lang.Long" column="STUDENT_ID" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="studentName">
<column name="STUDENT_NAME" sql-type="varchar(20)"/>
</property>
<set name="faculties" table="course" inverse="true" cascade="save-update">
<key column="FK_STUDENT_ID"/>
<many-to-many class="com.nielsenmedia.manytomany.entities.Faculty"
column="FK_FACULTY_ID"/>
</set>
</class>

When I create an instance of Faculty and add students to it and save, it saves both the Faculty and student table but not the course table. I expect it to create rows for the many-to-many association via 'course' table. Forgive if I miss anything so basic.

Regards,
-Jay.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 02, 2003 7:21 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Hi.
To be honest, I have no idea should your mapping work or not :)
First things come in mind looking at your code are:
1. you did not specify "unsaved-value" for your Course class. I suppose this it is mandatory for <composite-id> (see "8.4. Using cascading update()")
2. Make sure your Course class implements hashCode & equals. Mandatory for <composite-id> too. (see "6.4. As Composite Identifiers"
)

Also I have noticed none of your sets is lazy. That means when object is loaded, its set is also initialized which causes in turn loadind of other sets and so on. This may be VERY inefficient.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 02, 2003 9:51 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You have mapped both ends of the collection inverse="true".


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 12:58 am 
Newbie

Joined: Tue Dec 02, 2003 9:12 am
Posts: 7
As cavin and dimas said I gave the unsaved-value property, implemented the equals method, removed inverse property at both the ends and modified the composite-id as below
<class name="com.nielsenmedia.manytomany.entities.Course" table="course" dynamic-update="true" dynamic-insert="true" unsaved-value="any">
<composite-id unsaved-value="any">
<key-property name="studentID" column="FK_STUDENT_ID" type="long"/>
<key-property name="facultyID" column="FK_FACULTY_ID" type="long"/>
</composite-id>
</class>

and also the domain object accordingly.

I get rows created for every row that is created in the many to many end.

Now if I have attributes defined for the mapping table 'course' how will I go about providing values for these attributes. What if any of this attribute is not-null?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 6:00 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Take a look at documentation section 7 "Components" and particulary to 7.2 "In collections" - http://www.hibernate.org/hib_docs/refer ... nents-s2-2


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.