Mapping element via joining table with NHibernate

Posted by NhibernateIdiot on Stack Overflow See other posts from Stack Overflow or by NhibernateIdiot
Published on 2010-12-29T22:52:15Z Indexed on 2010/12/29 22:53 UTC
Read the original article Hit count: 188

Filed under:

This is stuff ive done lots of times before but my mind is just blanking at the moment, i will try and give a simple overview of my current situation.

I currently have 3 tables as shown below:

Office > id, name
Person > id, name
Office_Personnel > office_id, person_id

I then have a model for Person (id, name) and Office, however the Office model contains personnel information:

public class Office
{
 int Id {get;set;}
 string Name {get;set;}
 ICollection<Person> Personnel {get;set;}
}

Mapping person is easy, but now im a bit stumped as to why office wont map properly. I chose to use a set when I was mapping the Personnel as there shouldn't be any duplicates, however it doesn't seem to work as I would expect...

<set name="Personnel" table="office_personnel" cascade="all">
  <key column="office_id" />
  <one-to-many class="Person"/>
</set>

Now one thing that strikes me as odd is that there is no indication as to what person should be binding to (person_id). It keeps trying to find *office_id* column within the Person table.

I'm sure this is just some simple problem and im being an idiot, but any help would be great!

On a side note, I was weighing up if I should even bother having a middle man table, as I could directly put an Office_Id column within the Person table, but im not 100% sure if in my real project the Person class could be in multiple Offices further down the line...

© Stack Overflow or respective owner

Related posts about nhibernate-mapping