1

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...

0

1 Answer 1

1

In order to map this correctly, you need to either have 3 entities:

Person 1 <-> n Personnel n <-> 1 Office

or map it as a many-to-many association using join table (see section 8.3 of the hibernate docs)

 Person n <-> n Office
Sign up to request clarification or add additional context in comments.

1 Comment

To match his model, the latter it more adequate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.