views:

41

answers:

0

Hello,

I'm new to fluent nHiberbate and wanting to put into use in one of my projects. And I'm currently having trouble mapping a class that has both inheritance and self-referencing properties.

Here's my scenario:

I have an abstract class called Applicant

  public abstract class Applicant: IApplicant
  {
    public virtual long Id { get; private set; }

    public virtual string LastName { get; set; }
    public virtual string FirstName { get; set; }


   /// Other properties
  }

I also have an Employee, Spouse and Dependent subclasses derive from Applicant. Employee has a list of applicants (many-to-one relationship). Spouse and Dependent has a reference back to the Employee.

 public class Employee : Applicant
{
    public virtual string EmployeeId { get; set; }

    //family members (spouse/kids)
    public virtual IList<Applicant> Family { get; set; } 

}

 public class Spouse : Applicant
 {
    public virtual Applicant Employee { get; set; }

 }

  public class Dependent : Person 
  {
    public virtual Applicant Employee { get; set; }
  }

Basically, I want to use table-per-hierarchy for this Applicant class so that I can discriminate rows by an "ApplicantType" column. Also, the spouse and Dependent rows in the table should have the Employee ID in the ParentID column to point back to the Employee (will be null for the Employee rows)

I've got the mappings for the indivdual classes below, but I'm not sure if these will work for the situation?.

public class ApplicantMap : ClassMap<Applicant>
{
    public ApplicantMap()
    {

        Table("Applicants");

        Id(x => x.Id).GeneratedBy.Native();

        Map(x => x.LastName).Not.Nullable();
        Map(x => x.FirstName).Not.Nullable();


        DiscriminateSubClassesOnColumn("ApplicantType");

    }
}

public class EmployeeMap : SubclassMap<Employee>
{
    public EmployeeMap()
    {
        Map(x => x.EmployeeId);

        //other mappings

        HasMany(x => x.Family).Cascade.All().Inverse();
    }
}


public class SpouseMap : SubclassMap<Spouse>
{
    public SpouseMap ()
    {
       //Other mappings
        References(x => x.Employee).Cascade.All();
    }
}

public class DependentMap : SubclassMap<Dependent>
{
    public DependentMap ()
    {
       //Other mappings
        References(x => x.Employee).Cascade.All();
    }
}

Any help would be greatly appreciated !

-jb