Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table Per Subclass Inheritance mapping by NHibernate Mapping-by-Code

How to write mappings in new NHibernate Mapping-By-Code in Table Per Subclass strategy for this classes:

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; }
}
like image 427
Serg Melikyan Avatar asked Feb 21 '23 14:02

Serg Melikyan


1 Answers

Here is a possible mapping in a slighly abbreviated form

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

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

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

You don't need to duplicate declaration of the Id property in the derived classes. It's inherited from the parent Person class.

like image 115
hival Avatar answered May 01 '23 06:05

hival