-->
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: Simple mapping question
PostPosted: Mon Oct 31, 2005 10:10 pm 
Newbie

Joined: Mon Oct 31, 2005 10:04 pm
Posts: 5
I've been trying to solve a mapping problem for a few weeks with no luck. I know this is something simple, but I'm just not using the right terminology.

I have a Teacher class and a Student class (with corresponding database tables Teachers and Students). I have a TeacherStudents table that simply maps TeacherID to StudentID in a one-to-many fashion (one Teacher for many Students). I'd like to add an Advisor property to the Student class that uses the TeacherStudents table to get the Teacher mapped to the Student.

How would I go about this?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 01, 2005 4:50 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
It looks like your Teacher and Student are linked by a many-to-many relationship...

As a student can have many teachers, how do you choose the Advisor?

The classic solution to this problem is to add an Advisor column in your Students table (FK of Teachers) and map it as a many-to-one.

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 01, 2005 9:48 pm 
Newbie

Joined: Mon Oct 31, 2005 10:04 pm
Posts: 5
A Teacher can have many students, but a Student should be mapped to just one teacher. Adding an Advisor column to the Students table is a solution, but it seems like a duplication of the mapping that already exists in the TeacherStudents table.

What I'm looking for is something that would show up in the Students.hbm.xml file and look like this:

<relationship name="Advisor" table="TeacherStudents" my_column="StudentID" other_column = "TeacherID" class = "Teacher" />


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 11:24 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
I don't understand why you need the TeacherStudents table.

hansenm wrote:
A Teacher can have many students, but a Student should be mapped to just one teacher.


This is a <one-to-many> relationship:
- Add a 'Teacher' column in your Student table
- Add a MyTeacher/MyAdvisor property in the Student class (of the type Teacher) + Map it as a <many-to-one>
- Add an IList in your Teacher class and map it as a <bag inverse="true">
...

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 04, 2005 4:31 pm 
Newbie

Joined: Mon Oct 31, 2005 10:04 pm
Posts: 5
Thank you very much for the response. Using the bag solves a lot of similar issues I was having as well. I didn't quite understand its use before.


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.