-->
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.  [ 3 posts ] 
Author Message
 Post subject: Mapping problem
PostPosted: Sat Dec 08, 2007 7:53 am 
Newbie

Joined: Sat Dec 08, 2007 7:21 am
Posts: 2
Hi,

I'm new to NHibernate and currently using NHibernate 1.2.0.GA for a project.

I got a problem with my mapping (I think).
This is the situation...

Database
Persons
personID [PK]
name
sname

Students
studentID [PK]
personID [FK:Persons.personID]

Profs
profID [PK]
personID [FK:Persons.personID]

Classes
classID [PK]
className

StudentInClass
classID [FK:Classes.classID]
studID [FK:Students.studentID]

C#
Person, Student, Prof just got the same data members as in the database.
Class has a collection (List<Student> students) that holds all the student attending a class.

Map
Person
<class name="Person" table="Persons" lazy="false">
<id name="PersonID" column="personID" type="Int32" unsaved-value="0">
<generator class="identity" />
</id>

<property name="Name" column="name" type="String" length="50" />
<property name="SName" column="sname" type="String" length="15" />
</class>

Student
<joined-subclass name="Student" table="Students" lazy="false" extends="Person">
<key column="personID" />
<property name="StudID" column="studID" type="Int32" />
</joined-subclass>

Prof
<joined-subclass name="Prof" table="Profs" lazy="false" extends="Person">
<key column="personID" />
<property name="ProfID" column="profID" type="Int32" />
</joined-subclass>

Class
<class name="Class" table="Classes" lazy="false">
<id name="ClassID" column="classID" type="Int32" unsaved-value="0">
<generator class="identity" />
</id>

<property name="ClassName" column="className" type="String" length="5" />

<bag name="Students" table="StudentInClass" lazy="false">
<key column="classID"/>
<many-to-many class="Student" column="studID" />
</bag>
</class>

Problem
The problem is, when i ask for all the students I works fine. It links all the personID's in the student table to the right personID

in the person table.

However when i ask for all the classes and by this also asking for the students in those classes this get mixed up. It gets all the

classes and then the students in those classes (by the studID in StudenInClass table) then it links the students to the person

table. And this is where it fixes up. When linking to the person table it uses the studID to get record in the person table. It should

use the personID linked to the studentID in the student table.

Exp:
Person
[PersonID][Name][SName]
1 Do Jhone
2 Do Bob
3 Do Marc
4 Do Simon

Student
[StudID][PersonID]
1 2
2 4
3 1

Class
[ClassID][ClassName]
1 NHibernate Course
2 C# Course

StudentInClass
[StudID][ClassID]
1 1
1 2
2 1
3 1


The result should be:
NHibernate Course
=> Do Bob (studID 1, personID 2)
=> Do Simon (studID 2, personID 4)
=> Do Jhone (studID 3, personID 1)
C# Course
=> Do Bob (studID 1, personID 2)

But it's
NHibernate Course
=> Do Jhone (studID 1, personID 1)
=> Do Bob (studID 2, personID 2)
=> Do Marc (studID 3, personID 3)
C# Course
=> Do Jhone (studID 1, personID 1)

Can some1 help me with this problem?

Thanks in advanced
// Snelle Kenny


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 08, 2007 3:52 pm 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
I don't actually know that what you're trying to do is possible. I don't think you can use anything except for the primary key to define a joined-subclass.

If you still have control of this schema I would recommend removing the StudID column from your student table and then making the PersonID the primary key of the Student table. (The same would need to be done for the Profs).

Then everything will work as expected.

If you don't have control of your schema at this point you're probably stuck with a one-to-one mapping instead of inheritance. Personally I would much rather use inheritance though.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 08, 2007 8:46 pm 
Newbie

Joined: Sat Dec 08, 2007 7:21 am
Posts: 2
Thanks for your advice, by making the personID the PK instead of studID things work fine. ;)

// Snelle Kenny


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