Table Each subclass inheritance mapping by NHibernate mapping code

How to write the mapping in the new NHibernate Mapping-By-Code for each subclass strategy of this class:

public class Person
{
public virtual int Id {get; set; }
public virtual string Name {get; set; }
}

public class JuridicalPerson: Person
{
public virtual int Id {get; set; }
public virtual string LegalName {get; set; }
}

public class PrivatePerson: Person
{
public virtual int Id {get; set; }
public virtual bool Sex {get; set; }
}

This is a slightly abbreviated possible mapping

public class PersonMapping: ClassMapping
{
public PersonMapping()
{
Table("person");
Id(x => x.Id, m = > m.Generator(Generators.Native));
Property(x => x.Name);
}
}

public class JuridicalPersonMapping: JoinedSubclassMapping
{
public JuridicalPersonMapping()
{
Table("juridical_person");
Key(m => m.Column("person_id"));
Property(x => x.LegalName);
}
}

public class PrivatePersonMapping: JoinedSubclassMapping
{
public PrivatePersonMapping()
{< br /> Table("private_person");
Key(m => m.Column("person_id"));
Property(x => x.Sex);
}
}

You do not need to copy the Id attribute declaration in the derived class. It inherits from the parent class Person.

How to use this class Write the mapping in the new NHibernate Mapping-By-Code for each subclass strategy:

public class Person
{
public virtual int Id { get; set; }
public virtual string Name {get; set; }
}

public class JuridicalPerson: Person
{
public virtual int Id {get; set; }
public virtual string LegalName {get; set; }
}

public class PrivatePerson: Person
{
public virtual int Id {get; set; }
public virtual bool Sex {get; set; }
}

This is a slightly abbreviated form of possible mapping

< p>

public class PersonMapping: ClassMapping
{
public PersonMapping()
{
Table("person");
Id(x => x.Id, m => m.Generator(Generators.Native));
Property(x => x.Name);
}
}

public class JuridicalPersonMapping: JoinedSubclassMapping
{
public JuridicalPersonMapping()
{
Table("juridical_person");
Key(m => m.Column("person_id"));
Property(x => x.LegalName);
}
}

public class PrivatePersonMapping: JoinedSubclassMapping
{
public PrivatePersonMapping()
{
Table("private_person");
Key(m => m.Column("person_id"));
Property(x => x.Sex);
}
}

You do not need to copy the declaration of the Id property in the derived class. It inherits from the parent class Person.< /p>

Leave a Comment

Your email address will not be published.