Question

Forgive me I am a newbie in NHibernate, I found some example in hbm.xml like below.

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="test" >

      <class name="test.FieldSetMapping" table="fieldsetmapping" discriminator-value="2352" lazy="false">
        <id name="Id" column="fieldsetmapping_id" type="Int64">
            <generator class="test.NHibernate.IdGenerator, test">
              <param name="table">nextinstanceid</param>
              <param name="column">next</param>
              <param name="max_lo">9</param>
            </generator>
        </id>
        <discriminator column="mapping_type" type="Int32" />
        <version name="LastUpdateNumber" column="last_update_number" type="Int32" unsaved-value="-1"/>
        <property name="OwnerName" column= "owner_name" type="String"/>
        <component name="FieldSetDefinitionId" class="test.ObjectId">
          <property name="InstanceId" column= "fieldsetdefinition_id" type="Int64"/>
        </component>    
        <property name="OwnerTypeClassId" column= "owner_type" />
        <bag name="FieldMappings" cascade="all-delete-orphan" generic="true" lazy="false">
          <key column="fieldsetmapping_id" not-null="true" />
          <one-to-many class="test.FieldSet.FieldMapping, test" />
        </bag>
        <property name="MappingHandlerClass" column="handler_class" />
      </class>
      <subclass name="test.EntityFieldSetMapping, test" discriminator-value="2353"  extends="test.FieldSetMapping, test"  lazy="false" >
        <property name="TargetEntityType" type="test.Internal.NHibernate.ClassIdType, test" not-null="false">
          <column name="target_entity_type"/>
        </property>
      </subclass>
      <class ...>
        ...
      </class>
      <class ...>
         ...
      </class>
    </hibernate-mapping>

But I don't know what does discriminator mean . I check the Nhibernate Doc 5.1.6.

    The <discriminator> element is required for polymorphic persistence 
using the table-per-class-hierarchy mapping strategy and declares a discriminator 
column of the table. The discriminator column contains marker values that tell
 the persistence layer what subclass to instantiate for a particular row.A restricted
 set of types may be used: String, Char, Int32, Byte, Short, Boolean, YesNo, TrueFalse.

Does it mean if the mapping_type > 2352 the NH will initialize the subclass test.EntityFieldSetMapping for the row of the table fieldsetmapping? thanks.

Était-ce utile?

La solution

NHibernate will use the discriminator to detect which class needs to instantiate on a polymorphic scenario.

  • If mapping_type = 2352, it will create an instance of test.FieldSetMapping

  • if mapping_type = 2353, it will create an instance of test.EntityFieldSetMapping

Any other value should generate an exception.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top