代码之家  ›  专栏  ›  技术社区  ›  Mike Pone

带鉴别器的Hibernate映射

  •  7
  • Mike Pone  · 技术社区  · 16 年前

    我有一个表,其中一个字段可以根据描述符值(Project、TimeKeep或CostCenter)指向其他三个表之一的外键。通常这是通过子类实现的,我想知道下面的内容是否有效。 注意,子类名称与父类相同,noteObject属性映射到java.lang.Object类型的实例变量 因此,只要我们转换为正确的类型,它就应该接受Project、TimeKeep或CostCenter对象。hibernate允许这样做吗?谢谢

    <hibernate-mapping package="com.tlr.finance.mappings">
    
     <class name="AdminNotes" table="admin_notes">
        <id name="adminNoteId" column="admin_note_id" type="integer">
          <generator class="identity" />
        </id>
    
    <discriminator column="note_type" type="string" />
    
    <!-- make this property an enumerated type.  It is the discriminator -->
    <property name="adminNoteType" column="note_type" type="string" not-null="true" />
    <property name="adminNote" column="note" type="string" not-null="true" />
    <property name="adminNoteAdded" column="note_date" type="timestamp"
      not-null="true" /> 
    
    <subclass name="AdminNotes" discriminator-value="project" >
      <many-to-one name="noteObject" column="object_id" class="PsData" /><!-- Project -->
    </subclass>
    
    <subclass name="AdminNotes" discriminator-value="user" >
      <!-- rename timekeep to user -->
      <many-to-one name="noteObject" column="object_id" class="Timekeep" /><!-- user -->
    </subclass>
    
    <subclass name="AdminNotes" discriminator-value="costCenter" >
      <!-- rename timekeep to user -->
      <many-to-one name="noteObject" column="object_id" class="CostCenter" /><!-- cost center -->
    </subclass>
    
      </class>
    
    </hibernate-mapping>
    
    4 回复  |  直到 16 年前
        1
  •  9
  •   Nicolas Filotto    6 年前

    http://docs.jboss.org/hibernate/core/3.5/reference/en-US/html/mapping.html#mapping-declaration-discriminator

    元素是 多态持久性所必需的 表的鉴别器列。这个 鉴别器列包含标记 要为一个类实例化哪个子类 特定行。

    我不认为您能够为这些不同的含义使用单个AdminNote类。鉴别器在数据库级别用于帮助区分一个子类和另一个子类——它实际上不是java对象模型的一部分。

        2
  •  1
  •   Community Michael Schmitz    4 年前

    作为整数的鉴别器

    无法使用(…)将鉴别器值“TYPE”格式化为SQL字符串

    如果希望将鉴别器用作整数值,首先需要通过在class元素中设置鉴别器值属性,将基类的鉴别器值指定为整数:

    <class name="AdminNotes" table="admin_notes" abstract="true" discriminator-value= "-1">
    

    这将替换默认行为,即当找不到值时,鉴别器是类名。

    <hibernate-mapping package="com.tlr.finance.mappings">
    
        <class name="AdminNotes" table="admin_notes" abstract="true" discriminator-value= "-1">
            <id name="adminNoteId" column="admin_note_id" type="integer">
                <generator class="identity" />
            </id>
            <discriminator column="note_type" type="integer" />
    
            <!-- Make this property an enumerated type. It is the discriminator. -->
            <property name="adminNoteType" column="note_type" type="string" not-null="true" />
            <property name="adminNote" column="note" type="string" not-null="true" />
            <property name="adminNoteAdded" column="note_date" type="timestamp"
                      not-null="true" />
    
            <subclass name="AdminNotes" discriminator-value="0" entity-name="project">
                <many-to-one name="noteObject" column="object_id" class="PsData" /><!-- Project -->
            </subclass>
    
            <subclass name="AdminNotes" discriminator-value="1" entity-name="user">
                <!-- Rename timekeep to user -->
                <many-to-one name="noteObject" column="object_id" class="Timekeep" /><!-- user -->
            </subclass>
    
            <subclass name="AdminNotes" discriminator-value="2" entity-name="costCenter">
                <!-- Rename timekeep to user -->
                <many-to-one name="noteObject" column="object_id" class="CostCenter" /><!-- cost center -->
            </subclass>
        </class>
    </hibernate-mapping>
    
        3
  •  0
  •   Peter Mortensen TravisEz13    11 年前

    另外,如果您使用实体名称,您可以为子类映射重用Java类名。

    API ,或在Tuplizer中实现实体名称解析( Hibernate 3.3.2)基于adminNoteType字段。

    <hibernate-mapping package="com.tlr.finance.mappings">
    
        <class name="AdminNotes" table="admin_notes" abstract="true">
            <id name="adminNoteId" column="admin_note_id" type="integer">
              <generator class="identity" />
            </id>
    
            <discriminator column="note_type" type="string" />
    
            <!-- Make this property an enumerated type. It is the discriminator. -->
            <property name="adminNoteType" column="note_type" type="string" not-null="true" />
            <property name="adminNote" column="note" type="string" not-null="true" />
            <property name="adminNoteAdded" column="note_date" type="timestamp"
              not-null="true" />
    
            <subclass name="AdminNotes" discriminator-value="project" entity-name="project">
                <many-to-one name="noteObject" column="object_id" class="PsData" /><!-- Project -->
            </subclass>
    
            <subclass name="AdminNotes" discriminator-value="user" entity-name="user">
                <!-- rename timekeep to user -->
                <many-to-one name="noteObject" column="object_id" class="Timekeep" /><!-- user -->
            </subclass>
    
            <subclass name="AdminNotes" discriminator-value="costCenter" entity-name="costCenter">
                <!-- rename timekeep to user -->
                <many-to-one name="noteObject" column="object_id" class="CostCenter" /><!-- cost center -->
            </subclass>
        </class>
    </hibernate-mapping>
    
        4
  •  0
  •   Peter Mortensen TravisEz13    11 年前

    代替尝试使用继承能力来获取对正确类的引用,而是应该考虑使用 <any/> 使用 note_type 要确定 object_id noteObject

    有关 < ,见 Any type mappings "在 NHibernate 文档和博客文章 NHibernate Mapping