8.4. 複合識別子としてのコンポーネント

8.4. 複合識別子としてのコンポーネント

コンポーネントをエンティティクラスの識別子として使うことができます。コンポーネントクラスは以下の条件を満たす必要があります。

注記: Hibernate3 において、2番目の条件は絶対的な条件ではありません。それでもやはり条件を満たしてください。

複合キーを生成するために IdentifierGenerator を使用することはできません。代わりにアプリケーションが識別子を割り当てなくてはなりません。

通常の <id> 宣言の代わりに <composite-id> タグを (ネストされた <key-property> 属性と共に) 使います。以下の例では、 OrderLine クラスは Order の(複合)主キーに依存した主キーを持っています。

<class name="OrderLine">
    
    <composite-id name="id" class="OrderLineId">
        <key-property name="lineId"/>
        <key-property name="orderId"/>
        <key-property name="customerId"/>
    </composite-id>
    
    <property name="name"/>
    
    <many-to-one name="order" class="Order"
            insert="false" update="false">
        <column name="orderId"/>
        <column name="customerId"/>
    </many-to-one>
    ....
    
</class>

このとき、 OrderLine テーブルへ関連する外部キーもまた複合です。他のクラスのマッピングでこれを宣言しなければなりません。 OrderLine への関連は次のようにマッピングされます。

<many-to-one name="orderLine" class="OrderLine">
<!-- the "class" attribute is optional, as usual -->
    <column name="lineId"/>
    <column name="orderId"/>
    <column name="customerId"/>
</many-to-one>

<column> タグはどこであっても column 属性の代わりになります。)

OrderLine への many-to-many 関連も複合外部キーを使います。

<set name="undeliveredOrderLines">
    <key column name="warehouseId"/>
    <many-to-many class="OrderLine">
        <column name="lineId"/>
        <column name="orderId"/>
        <column name="customerId"/>
    </many-to-many>
</set>

Order にある OrderLine のコレクションは次のものを使用します。

<set name="orderLines" inverse="true">
    <key>
        <column name="orderId"/>
        <column name="customerId"/>
    </key>
    <one-to-many class="OrderLine"/>
</set>

<one-to-many> 属性は、例によってカラムを宣言しません)

OrderLine 自身がコレクションを持っている場合、同時に複合外部キーも持っています。

<class name="OrderLine">
    ....
    ....
    <list name="deliveryAttempts">
        <key>   <!-- a collection inherits the composite key type -->
            <column name="lineId"/>
            <column name="orderId"/>
            <column name="customerId"/>
        </key>
        <list-index column="attemptId" base="1"/>
        <composite-element class="DeliveryAttempt">
            ...
        </composite-element>
    </set>
</class>