8

記述子の値(Project、TimeKeep、またはCostCenter。通常、これはサブクラスで実装されます。私が持っているものがあるかどうか疑問に思っています)に基づいて、他の3つのテーブルのいずれかで外部キーを指すことができる1つのフィールドを持つテーブルがあります。サブクラス名は親クラスと同じであり、noteObjectプロパティはjava.lang.Objectタイプのインスタンス変数にマップされるため 、にキャストする限り、Project、TimeKeep、またはCostCenterオブジェクトのいずれかを受け入れる必要があります。正しいタイプです。休止状態でこれが可能になりますか?ありがとうございます。

<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

4 に答える 4

9

ディスクリミネーターは、クラス階層を単一のテーブルに格納するために使用されます。あなたが持っているのは、複数の意味を持つ単一のクラスです。

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

この要素は、table-per-class-hierarchy マッピング戦略を使用したポリモーフィック永続性に必要であり、テーブルの識別子列を宣言します。ディスクリミネーター列には、特定の行に対してどのサブクラスをインスタンス化するかを永続化レイヤーに伝えるマーカー値が含まれています。

これらの異なる意味ごとに単一の AdminNote クラスを使用できるとは思いません。ディスクリミネータはデータベース レベルで使用され、サブクラスを別のサブクラスと区別するのに役立ちます。これは、実際には Java オブジェクト モデルの一部ではありません。

各識別子の値に対して 1 つずつ、AdminNote の複数のサブクラスを定義する必要があります。

于 2008-10-06T16:28:31.997 に答える
1

整数としての識別子

通常、サブクラスで識別子の値を整数として指定すると、エラーが発生します

(...) を使用して、識別子の値 'TYPE' を SQL 文字列にフォーマットできませんでした

ディスクリミネータを整数値として使用する場合は、まずクラス要素で discriminator-value 属性を設定して、基本クラスに整数として指定する必要があります。

<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>
于 2010-07-07T12:23:31.527 に答える
0

私の知る限り、エンティティ名を使用すると、サブクラス マッピングに Java クラス名を再利用できます。

以下のマッピングを試してください。ここでは、スーパー クラス マッピング自体が抽象的です。サブクラスは、サブクラスごとに同じ 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>
于 2009-07-10T12:48:02.390 に答える
0

継承機能を使用して適切なクラスへの参照を取得しようとする代わりに、 を使用してが参照している型を特定し、値を適切なオブジェクト参照に設定する<any/>マッピング型を使用することを検討する必要があります。note_typeobject_idnoteObject

の詳細については、 NHibernate<any/>のドキュメントの「Any type mappings」およびブログ投稿NHibernate Mappingを参照してください。

于 2009-08-28T21:16:23.920 に答える