Question

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

Was it helpful?

Solution

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);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top