Nhibernate table for class hierarchy, mapping members of a derived class that hide base class elements

class FooBase{...}
class FooDerived : FooBase {...}

class BaseContainer
{
    public virtual FooBase Foo {get;set;}
}

class DerivedContainer : BaseContainer
{
    public virtual new FooDerived Foo {get;set;}
}

Hibernate mapping options Option 1 below Cannot save to / from NHibernate, generating an additional member declaration in xml (index error out of range)

   <class name="BaseContainer" discriminator-value="0">

         <discriminator column="ContainerType" type="int" />

         <many-to-one name="Foo"
                      foreign-key="..."
                      class="FooBase"
                      column="FooId"
                      unique="true"/>

          <subclass name="DerivedContainer"  discriminator-value="1">

               <many-to-one name="Foo"
                            foreign-key="..."
                            class="FooDerived"
                            column="FooId"

                            unique="true"/>
          </subclass>

   </class>

Option 2 independent comparisons! Fetch operation is erroneous, does not distinguish between types

   <class name="BaseContainer" discriminator-value="0">

         <discriminator column="ContainerType" type="int" />

         <many-to-one name="Foo"
                      foreign-key="..."
                      class="FooBase"
                      column="FooId"
                      unique="true"/>

   </class>

   <class name="DerivedContainer"  discriminator-value="1">

        <many-to-one name="Foo"
                     foreign-key="..."
                     class="FooDerived"
                     column="FooId"
                     unique="true"/>

  </class>

Stuck, I would be grateful for any pointers, although I understand that this can be easily achieved if you do this through a table for each subclass, is there a way that can be achieved through a table for a class hierarchy

+3
source share

All Articles