views:

163

answers:

1

Hi,

i want to map two classes m:n associated using NHibernate. NH would map a simple m:n association in a link table with foreign key constraints to the entity tables. Now I want to attach more attributes to the association as seen on this example: UML example - association class

(and I want NHibernate to store these attributes in the link table) UML example - sample model without association class This UML diagram shows how you would "resolve" the association class for programming. Up to now, I did this myself and coded 3 classes Person, Book and PersonBookLink with PersonBookLink having the returnDate attribute. NHibernate is fine with the mapping, but I'm looking for a less complex way to navigate through this model.

How would you do the mapping in NHibernate?

+1  A: 

There isn't a less complex way. A many-to-many relationship with extra attributes is mapped as two one-to-many relationships. You will have to decide which one side manages the relationship and set the inverse attribute in your mapping on the other one side so that NHibernate doesn't try to insert the link record twice.

I would model this as Person, Book, and Lending and let Person control the relationship (Person borrows Book).

PS - I would also use this project as an opportunity to write a Librarian class with a Shhhh(IEnumerable<Person> patrons) method.

Jamie Ide
Also known as an "objectified relationship."
Daniel Auger

related questions