views:

399

answers:

2

I have two items A and B, which have a uni directional one-to-one relationship. (A has one B)

In the database these are represented by ATable and BTable, and they are linked together by ABTable. (From the database setup it appears there is a many-to-many relationship but there is not, it was done this way for normalization reasons).

The problem is due to this setup, I have only been able to get NHibernate to map this as a many-to-many relationship between the entities. Is there anyway of making the entities have a one-to-one relationship?

The best I could think of is to leave its has a many to many relationship, and then have two properties on the A entity one that returns a List of B, which would satisfy the mapping and a second non-mapped property that would get the first B from the list, to satisfy my application. - but this seems un-eligant.

A: 

You might try combining the join-table with one-to-one mappings in various ways. A join-table mapping permits a single class to be persisted across more than one table which have a one-to-one relationship.

Justice
+1  A: 

Are you sure you mean a one-to-one? I've had so many people ask for one-to-one's when they really mean many-to-one's.

Anyway, short of changing your schema, the easiest thing is what you suggested; however, to make it a little cleaner, you can make the collections private so you're only exposing the two properties that fetch the first item. You can see the various methods in Fluent NHibernate for mapping private properties on the wiki.

James Gregory
Yeah pretty sure its one-to-one A has one B, each B can only be associated with one A.Good call on the private collections, I wasn't aware mapped properties could be private.
Dan