I have this table structure and would like to map it using Fluent Hibernate (subclass if possible). I cannot change the structure because the database has too many records and might cause major applications rework. It would be easier if the Id from Party table was a foreign key in person and organization table, but in the particular scenario the database has person and organization key as a foreign key in party table. Any help would be great.
Party table
Id
PersonId
OrganizationId
Person table
Id
FName
LName
Organization table
Id
OrgName
OrgDescription
You can’t map a linking table with the automapping if it contains any fields other than the two ids that make up the composite id. In your case, your Party table has an Id field which breaks the automapping rules because it’s not well-designed (i.e. a composite id shouldn’t have an auto-incrementing id, although we do this for indexing where I work).
To fix this, you’ll just have to create a ClassMap for Party, and map Id as the Id and reference Person and Organization.
Then, in your Person and Organization entities, instead of creating a HasManyToMany mapping to Party, you’d create a HasMany to the Party entity.
Really, what you’re doing is explicitly matching the code to look more like and ERD, whereas the automapping implies many-to-many relationships through a link table only if it contains only a composite primary key.
This stumped me for three days and I took this route as a “hack”, only to later read this explanation in Fluent NHibernate’s google group a couple of weeks ago.
I hope that helps, if not I can throw together some code for you.
See also my post about the same situation.
edit:
Here is how this would look at a fairly high level. Remember, you’ll have to initialize your collections in your entity constructors and create setter methods. See Getting Started