0

I want to create tow entities for the same table without discriminator. Lets sey that my table represent Person and I would like to create 2 entity classes: Person and PersonExt. Person will contain most of the table columns like : name, Age, Adress ext. and the PersonExt class will inharit from the Person class and will contain mappinge to the rest of the columns in the Person Table. I don't want to create an additional mapping file for the PersonExt that will conatin mapping to all the Person filed again - but just mappings for the rest of the columns that I've not mapped to yet. Is it possible with NHibrnate? and if so, can you explain how?

Public Class Person
{
   public virtual string Name {get;set;}
   public virtual int Age {get;set;}
   public virtual string Address{get;set;}
}

Public class PersonExt:Person
{
   public virtual int NumOfChildren {get;set;}
   public virtual string FamilyStatus {get;set;}
   ......
}
dgw
  • 13,418
  • 11
  • 56
  • 54
  • In Fluent NHibernate you can do this with ClassMap inheritance but I'm not sure that you can with straight up xml mappings. – Cole W Mar 27 '12 at 11:47
  • 1
    @ColeW - Can using the 'joined-subclass' element -> http://simsonlive.wordpress.com/2008/03/09/how-inheritance-works-in-hibernate/ – SeanCocteau Mar 27 '12 at 12:43
  • @SeanCocteau that would create an inheritance hierarchy and would confuse schemaexport because it maps to the same table. ColeW is right with his answer – Firo Mar 27 '12 at 18:15

1 Answers1

0

Yes using Fluent mapping. e.g.

public abstract class PersonBaseMap<TPerson> : ClassMap<TPerson> where TPerson : Person
{
    public PersonBaseMap()
    {
       Map(p => p.Name);
       Map(p => p.Age);
       Map(p => p.Address);
    }
}

public class PersonMap : PersonBaseMap<Person>
{
}

public class PersonExtMap : PersonBaseMap<PersonExt>
{
    public PersonExtMap()
    {
       Map(p => p.NumOfChildren);
       Map(p => p.FamilyStatus);
    }
}
Firo
  • 30,626
  • 4
  • 55
  • 94