Components as composite identifiers in Hibernate by R4R Team

In the Hibernate we can use a component as an identifier of an entity class. Then out component class must satisfy certain requirements:

1. It must implement java.io.Serializable.
2. It must re-implement equals() and hashCode() consistently with the database's notion of composite key equality.

We have to always remember that is: In Hibernate, second requirement is not an absolutely hard requirement of Hibernate, it is recommended.

Hibernate do not allow the use an IdentifierGenerator to generate composite keys. Instead the application must assign its own identifiers. For this we need to Use the <composite-id> tag, with nested <key-property> elements, in place of the usual <id> declaration. 

For example, the OrderLineItem class has a primary key that depends upon the (composite) primary key of Order.

<class name="OrderLineItem">
    <composite-id name="id" class="OrderLineIdItem">
        <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>

Any foreign keys referencing the OrderLineItem table are now composite. So need to Declare this in your mappings for other classes. An association to OrderLineItem is mapped like this:

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

The column element is an alternative to the column attribute everywhere. Using the column element just gives more declaration options, which are mostly useful when utilizing hbm2ddl
A many-to-many association to OrderLineItem also uses the composite foreign key:

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

The collection of OrderLinesItem in Order would use:

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

The <one-to-many> element declares no columns. If OrderLine itself owns a collection, it also has a composite foreign key.

<class name="OrderLineItem">
    ....
    ....
    <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>
Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!