简体   繁体   中英

Inheritance in Nhibernate -mapping file

    public int Id { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime ModifiedDate { get; set; }

I have noticed that the above fields are repeating in all of my tables. So I wish to create a class called 'public abstract class AuditableEntity',for all this fields. Suppose I want to create a table for "Product" with an extra filed "Price" Like

public class Product:AuditableEntity{
public int Price { get; set; }
}

How should I create xml mapping file for nhibernate.

My database will be oracle. I am new to nhibernate I am doing xml mapping ...

You could simply map the fields in the AuditableEntity directly in the Product xml mapping (so that the Product.hbm.xml maps Id , CreatedDate , ModifiedDate , Price )

I have found at least one example here: https://forum.hibernate.org/viewtopic.php?f=1&t=960519 with hibernate (the Java "original" version of NHibernate) of using DTD to import another XML (containing the "common" columns)... But I haven't tested it in NHibernate. Mmmmh... Not supported anymore: https://nhibernate.jira.com/browse/NH-1236

if you are doing xml mapping, there's no inheritance on the mapping side because you don't inherit xml files.

I do have a base entity class

public abstract class EntityBase<TId>
{
    #region Properties

    public virtual TId Id { get; private set; }

    #endregion

    public override Boolean Equals(object obj)
    {
        return Equals(obj as EntityBase<TId>);
    }

    private static Boolean IsTransient(EntityBase<TId> obj)
    {
        return obj != null &&
            Equals(obj.Id, default(TId));
    }

    private System.Type GetUnproxiedType()
    {
        return GetType();
    }

    public virtual Boolean Equals(EntityBase<TId> other)
    {
        if (other == null)
            return false;

        if (ReferenceEquals(this, other))
            return true;

        if (!IsTransient(this) && !IsTransient(other) && Equals(Id, other.Id))
        {
            var otherType = other.GetUnproxiedType();
            var thisType = GetUnproxiedType();

            return thisType.IsAssignableFrom(otherType) ||
                otherType.IsAssignableFrom(thisType);
        }

        return false;
    }

    public override Int32 GetHashCode()
    {
        return Equals(Id, default(TId)) ? base.GetHashCode() : Id.GetHashCode();
    }
}

public abstract class EntityBase : EntityBase<Guid>, IStampedEntity
{
    public virtual DateTime CreateDate { get; set; }
    public virtual String CreateUser { get; set; }
    public virtual DateTime LastUpdateDate { get; set; }
    public virtual String LastUpdateUser { get; set; }
}

and a base mapping class

public abstract class MapBase<T> : ClassMapping<T> where T : EntityBase
{
    protected MapBase()
    {
        Id(x => x.Id, map =>
        {
            map.Column("Id");
            map.Generator(Generators.GuidComb);
        });

        Property(x => x.CreateDate, map => map.Type(NHibernateUtil.DateTime2));
        Property(x => x.CreateUser);
        Property(x => x.LastUpdateDate, map => map.Type(NHibernateUtil.DateTime2));
        Property(x => x.LastUpdateUser);
    }
}

for doing this with mapping by code.

If you are new to nhibernate, i would move to mapping by code and forget xml mapping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM