I recently started the Asp Net Core project with NHibernate as an orm. I have definitely little experience with NHibernate. I am looking for an equivalent for the @MappedSuperclass Hibernate annotation. Either I can't find a good solution or I can't use any found. But from the beginning: Each of my entities inherits from the base class BaseEntity, containing several technical fields / columns
public class BaseEntity
{
public virtual long id { get; set; }
public virtual DateTime inDate { get; set; }
public virtual DateTime? outDate { get; set; }
//some other properies
}
public class AnyClass : BaseEntity
{
//some AnyClass properties
}
public class AnyClassMap : ClassMapping<AnyClass>
{
public AnyClassMap()
{
Table("any_table");
//Copy/Paste to each mapping class
Id(b => b.id, x => { x.Column("id"); x.Generator(Generators.Native); x.Type(NHibernateUtil.Int64); });
Property(b => b.inDate, x => { x.Column("in_date"); });
Version(b => b.outDate, x => { x.Column("out_date"); x.Type(NHibernateUtil.DbTimestamp); });
//Some other BaseEntity properties
//some AnyClass properties
}//ctor
}
In every mapping class I repeat mappings about the properties of the base class. However, I would like to achieve something similar to:
public class BaseEntityMap : MappedSuperclassEquvalent<BaseEntity>
{
public BaseEntityMap()
{
Id(b => b.id, x => { x.Column("id"); x.Generator(Generators.Native); x.Type(NHibernateUtil.Int64); });
Property(b => b.inDate, x => { x.Column("in_date"); });
Version(b => b.outDate, x => { x.Column("out_date"); x.Type(NHibernateUtil.DbTimestamp); });
//Some other BaseEntity properties
}//ctor
}
public class AnyClassMap : ClassMapping<AnyClass>
{
public AnyClassMap()
{
Table("any_table");
//example only
IncludeMapping(BaseEntityMap.Class);
//some AnyClass properties
}//ctor
}
I know that I can use BaseEntity as ComponentMapping and then use it in the AnyClass class as property. However, ComponentMapping does not allow id and version mapping. On the other hand, for many reasons I care about inheritance from the base entity.
Thank you in advance
Just create base mapping class based on ClassMapping
with all stuff you need:
public class BaseEntityClassMapping<TBaseEntity>: ClassMapping<TBaseEntity> where TBaseEntity: BaseEntity
{
public BaseEntityClassMapping()
{
Id(b => b.id, x => { x.Column("id"); x.Generator(Generators.Native); x.Type(NHibernateUtil.Int64); });
Property(b => b.inDate, x => { x.Column("in_date"); });
Version(b => b.outDate, x => { x.Column("out_date"); x.Type(NHibernateUtil.DbTimestamp); });
//Some other BaseEntity properties
}
}
And make your mappings based on this class:
public class AnyClassMap : BaseEntityClassMapping<AnyClass>
{
public AnyClassMap()
{
Table("any_table");
//some AnyClass properties
}
}