views:

145

answers:

1

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

A: 

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

public class Party {
    public virtual int Id { get; set; }
    public virtual Person Person { get; set; }
    public virtual Organization Organization { get; set; }
}
public class PartyMap : ClassMap<Party> {
    public PartyMap() {
        Id(x => x.Id);
        References(x => x.Person);
        References(x => x.Organization);
    }
}
public class Person {
    public virtual int Id { get; set; }
    public virtual string FName { get; set; }
    public virtual string LName { get; set; }
    public virtual ICollection<Party> Parties { get; set; }
}
public class PersonMap : ClassMap<Person> {
    public PersonMap() {
        Id(x => x.Id);
        Map(x => x.FName);
        Map(x => x.LName);
        HasMany(x => x.Parties);
    }
}
public class Organization {
    public virtual int Id { get; set; }
    public virtual string OrgName { get; set; }
    public virtual string OrgDescription { get; set; }
    public virtual ICollection<Party> Parties { get; set; }
}
public class OrganizationMap : ClassMap<Organization> {
    public OrganizationMap() {
        Id(x => x.Id);
        Map(x => x.OrgName);
        Map(x => x.OrgDescription);
        HasMany(x => x.Parties);
    }
}
Jim Schubert
Thank you Jim. Its funny that I was looking at the Party table all this time and did not realized that Party table is a link table, The auto increment Id in that table was throwing me off track. Thing is, the data in Party table has constrained where you can have either have PersonId or OrganizationId, it kinda tries to map it as one to one relationship, so I agree with you whoever designed it probably missed this issue. I am new to NHibernate, I'll be very happy if you provided some code. Greatly appreciated.Rusty
RM
@Rusty: I was a lot busier recently than I had anticipated. So, I whipped up this code and I hope it helps get you pointed in the right direction. I'm sure there are other ways to do this (using a convention for the Party class, maybe?) but this was the easiest way for me, since all of my linking tables also have audit fields, notes fields, and other random crap just to link two tables together.
Jim Schubert