鉴别器用于什么?

时间:2014-03-04 12:58:32

标签: nhibernate

原谅我,我是NHibernate的新手,我在hbm.xml中找到了一些例子,如下所示。

    <?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>

但我不知道鉴别器是什么意思。我查看了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.

是否表示mapping_type&gt; 2352 NH将为表test.EntityFieldSetMapping的行初始化子类fieldsetmapping?感谢。

1 个答案:

答案 0 :(得分:3)

NHibernate将使用鉴别器来检测哪个类需要在多态方案中实例化。

  • 如果mapping_type = 2352,则会创建test.FieldSetMapping

  • 的实例
  • 如果mapping_type = 2353,它将创建test.EntityFieldSetMapping

  • 的实例

任何其他值都应该生成异常。