nhibernate subclass in code

Posted by Antonio Nakic Alfirevic on Stack Overflow See other posts from Stack Overflow or by Antonio Nakic Alfirevic
Published on 2010-05-27T11:32:11Z Indexed on 2010/06/08 13:52 UTC
Read the original article Hit count: 376

Filed under:

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:(

© Stack Overflow or respective owner

Related posts about nhibernate