Next Previous Index

13. Composite Id


Definition:

Composite Identifiers are PK identifiers for first-rank classes that consist of more than one column.


Scenario:

Foo has a primary key that is of type Person. Person is made up of a String name and an Address type.


Person Foo.getId()   // PK is multi-column mapped 

Hibernate Mapping:

In Hibernate, this could be mapped as follows:


<class name="Foo" table="foo">
    <composite-id name="id" class="Person">
        <key-property name="name" type="string" column="name"/>
        <key-many-to-one name="address" class="Address" column="addr_id"/>
    </composite-id>
    <property name="age" column="age" type="string"/>
</class>

<class name="Address" table="address">
    ...
</class>

Foo has a composite id of type Person. This is mapped to two columns: 1) Name, 2) a many-to-one relationship with the Address class.


Either <key-property> or <key-many-to-one> declarations may exist within a composite id.


Table Schema:

Foo

name

addr_id

age

Address

id


So Foo has two PK-columns: addr_id and name. These are combined to initialise Person instances.


Bidirectionality:

Bidirectionality has no meaning here.



Next Previous Index