views:

13

answers:

0

Hi, I am trying to get this FluentNHibernate mapping to work. I have three tables Person, Employee and Employer. The Employee table extends the attributes of the Person table, and it's primary key is a foreign key to the Person table.

The Employee table also has a foriegn key to the Employer table. An employer can have many employees, and each employee is a person.

I have used FluentNHibernate to map these three tables. I used a Joined Subclass mapping for Employee -> Person.

I am trying to get a mapping to work so that I can get employers and their associated employees (eager fetching), but the generated select statement selects the EmployerId on both the Employee table (good), and the Person table (bad) resulting in an error (see below).

SELECT employees0_.EmployerId as Employee5_2_, employees0_. PersonId as PersonId2_, employees0_.PersonId as PersonId1_1_, employees0_.FirstN ame as FirstName1_1_, employees0_.LastName as LastName1_1_, employees0_1_.PayRat e as PayRate2_1_, employees0_1_.EmployerId as EmployerId2_1_, employer1_.Employe rId as EmployerId0_0_, employer1_.Name as Name0_0_ FROM [Person] employees0_ lef t outer join Employee employees0_1_ on employees0_.PersonId=employees0_1_.Employ eeId left outer join [Employer] employer1_ on employees0_1_.EmployerId=employer1 .EmployerId WHERE employees0.Employee.EmployerId=@p0;@p0 = 1

Please find the schema, class and Fluent Mappings below. What have I done wrong?

I have defined classes for these tables as follows:

public class Person
{
    public int PersonId { get; set; }
    public string PersonClassification { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class Employer
{
    public int EmployerId { get; set; }
    public string Name { get; set; }
    public IList<Employee> Employees { get; set; }
}

public class Employee : Person
{
    public Employer Employer { get; set; }
    public decimal PayRate { get; set; }

}

public class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {
        Id(x => x.PersonId).Column("PersonId").GeneratedBy.Identity();
        Map(x => x.FirstName);
        Map(x => x.LastName);
        DiscriminateSubClassesOnColumn("PersonClassification");
    }
}

public class EmployeeMap : SubclassMap<Employee>
{
    public EmployeeMap()
    {
        this.DiscriminatorValue("Employee");

        Join
            (
                "Employee",
                join =>
                {
                    join.Optional();
                    join.KeyColumn("EmployeeId");
                    join.Map(x => x.PayRate);
                    join.References(x => x.Employer).Column("EmployerId");
                }
            );
    }
}

public class EmployerMap : ClassMap<Employer>
{
    public EmployerMap()
    {
        Id(x => x.EmployerId).Column("EmployerId").GeneratedBy.Identity();
        Map(x => x.Name);
        HasMany(x => x.Employees).KeyColumn("Employee.EmployerId");
    }
}