tags:

views:

34

answers:

1

I would like to set up table-per-classhierarchy inheritance in nhibernate thru code. Everything else is set in XML mapping files except the subclasses. If i up the subclasses in xml all is well, but not from code. This is the code i use - my concrete subclass never gets created:(

//the call
NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();
SetSubclass(config, typeof(TAction), typeof(tActionSub1), "Procedure");

//the method
public static void SetSubclass(Configuration configuration, Type baseClass, Type subClass, string discriminatorValue)
{
            PersistentClass persBaseClass = configuration.ClassMappings.Where(cm => cm.MappedClass == baseClass).Single();
            SingleTableSubclass persSubClass = new SingleTableSubclass(persBaseClass);
            persSubClass.ClassName = subClass.AssemblyQualifiedName;
            persSubClass.DiscriminatorValue = discriminatorValue;
            persSubClass.EntityPersisterClass = typeof(SingleTableEntityPersister);
            persSubClass.ProxyInterfaceName = (subClass).AssemblyQualifiedName;
            persSubClass.NodeName = subClass.Name;
            persSubClass.EntityName = subClass.FullName;
            persBaseClass.AddSubclass(persSubClass);
}

the Xml mapping looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Riz.Pcm.Domain.BusinessObjects" assembly="Riz.Pcm.Domain">
  <class name="Riz.Pcm.Domain.BusinessObjects.TAction, Riz.Pcm.Domain" table="dbo.tAction" lazy="true">
    <id name="Id" column="ID">
      <generator class="guid" />
    </id>
    <discriminator type="String" formula="(select jt.Name from TJobType jt where jt.Id=JobTypeId)" insert="true" force="false"/>
    <many-to-one name="Session" column="SessionID" class="TSession" />
    <property name="Order" column="Order1" />
    <property name="ProcessStart" column="ProcessStart" />
    <property name="ProcessEnd" column="ProcessEnd" />
    <property name="Status" column="Status" />
    <many-to-one name="JobType" column="JobTypeID" class="TJobType" />
    <many-to-one name="Unit" column="UnitID" class="TUnit" />
    <bag name="TActionProperties" lazy="true" cascade="all-delete-orphan" inverse="true" >
      <key column="ActionID"></key>
      <one-to-many class="TActionProperty"></one-to-many>
    </bag>
    <!--<subclass name="Riz.Pcm.Domain.tActionSub" discriminator-value="ZPower"></subclass>-->
  </class>
</hibernate-mapping>

What am I doing wrong? I can't find any examples on google:(

A: 

We are using FluentNHibernate for mapping. The User entity is a base class for Learner, Assessor, Admin, etc.

Here is an example:

 public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        this.Id(x => x.Id);

        this.Map(x => x.Active);

        this.Component(
            x => x.Address,
            m =>
            {
                m.Map(x => x.Address1).Length(512);
                m.Map(x => x.Address2);
                m.Map(x => x.Address3);
                m.Map(x => x.Address4);
                m.Map(x => x.City);
                m.Map(x => x.County);
                m.Map(x => x.PostCode);
                m.References(x => x.Country);
            });

        this.References(x => x.CreatedBy);

        this.Map(x => x.CreatedDate).Not.Nullable();

        this.DiscriminateSubClassesOnColumn("className").Length(64);
    }
}

and derived class

public class LearnerMap : SubclassMap<Learner>
{
    #region Constructors and Destructors

    /// <summary>
    /// Initializes a new instance of the <see cref="LearnerMap"/> class.
    /// </summary>
    public LearnerMap()
    {
        this.Map(x => x.DateOfBirth);
        this.Map(x => x.NINumber);
        this.Map(x => x.IscCodeId);
        this.Map(x => x.ScnNo);
        this.Map(x => x.ULN);


        this.HasManyToMany(x => x.Organisation)
            .ParentKeyColumn("userId")
            .ChildKeyColumn("organisationId").Table(
            "UserOrganisations");
    }

Hope that this will help

isuruceanu