-->
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.  [ 10 posts ] 
Author Message
 Post subject: many to many two tables?
PostPosted: Sun Jan 25, 2009 5:26 am 
Newbie

Joined: Sun Jan 25, 2009 4:32 am
Posts: 8
Is there a way to do a many to many for two tables?

Lets say I have a School table, SchoolToPeople link table, Professor table, Student Table.

The link could be:
School -> SchoolToPeople (schoolId, personId) -> Professor (professorId)
or
School -> SchoolToPeople (schoolId, personId) -> Student (studentId)

I've tried...

<bag name="Professors" table="SchoolToPeople" lazy="true" >
<key column="schoolId"></key>
<many-to-many class="Professor" column="personId" />
</bag>

<bag name="Students" table="SchoolToPeople" lazy="true" >
<key column="schoolId"></key>
<many-to-many class="Student" column="personId" />
</bag>


This results in getting an error saying "No row with the given identifier exists"

There doesn't seem to be a way to tell the many to many mapping that personId is in SchoolToPeople and studentId is for Student.


Help on this would be greatly appreciated!


Thanks,
Stephen

P.S. No I'm not a student in college or something. I'm just using this as an example.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 25, 2009 10:05 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
The mapping looks ok. Is it possible that there are no Professors or Students for a Scholl ? Then you have to add not-found="ignore" to the many-to-many definition.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 25, 2009 2:33 pm 
Newbie

Joined: Sun Jan 25, 2009 4:32 am
Posts: 8
No, the problem is that since SchoolToPeople uses personId and Professor uses professorId. Since I've defined it to use personId, it seems like its trying to find personId in the Professor table, which doesn't exist.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 25, 2009 5:50 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
And what's the relation between personId and professorId ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 25, 2009 7:20 pm 
Newbie

Joined: Sun Jan 25, 2009 4:32 am
Posts: 8
The relation can go two directions from School

School(schoolId) -> SchoolToPeople(schoolId, personId) -> Professor(professorId)

or

School(schoolId) -> SchoolToPeople(schoolId, personId) -> Student(studentId)


I think I found the problem, but I haven't been able to find a solution for this just yet. What seems to be happening is it's doing a left outer join to professor from SchoolToProfessor, thus returning two records. one of them being null, since the null one points to a student, not a professor.

If nhibernate did this join using an inner join, then the null personId wouldn't be returned.

I hope this clarifies it some.


Thanks,
Stephen


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 3:34 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You probably need to separate join tables SchoolToStudent and SchoolToProfessor. Or you use one bag for students and professors and define separate access properties which will do the separation.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 4:05 am 
Newbie

Joined: Sun Jan 25, 2009 4:32 am
Posts: 8
I was afraid I might have to separate out the table into two. It's unfortunate you can't use inner join instead of left outer join. If this was possible, this would deffinately solve it.

I'll look into the bag access idea. Would you mind providing me an example of this type of config?

Thanks,
Stephen


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 5:41 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
The "shared" bag will only work if Students and Professors are stored in the same table. If that's the case, you can map one bag with People instead of two separate bags for Students and Professors.

Then you define properties on your class:

IList<Professor> Professors
{
get { return the Professors from the People List }
}

and the same for Students. Adding and removing objects from these lists will be the tricky part. So I recommend using two tables.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 12:34 pm 
Newbie

Joined: Sun Jan 25, 2009 4:32 am
Posts: 8
Thanks. As I said before, if we could force a many to many to do an inner join, then this would solve this problem.

You wouldn't happen to know why they choose left outer join instead of inner join?


Thanks again,
Stephen


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2009 2:44 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can generally disable outer join fetching, but that's a global setting and not for a special class.

_________________
--Wolfgang


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