Questo contenuto non è disponibile nella lingua selezionata.
8.4. Components as composite identifiers
You can use a component as an identifier of an entity class. Your component class must satisfy certain requirements:
- It must implement
java.io.Serializable
. - It must re-implement
equals()
andhashCode()
consistently with the database's notion of composite key equality.
Note
In Hibernate3, although the second requirement is not an absolutely hard requirement of Hibernate, it is recommended.
You cannot use an
IdentifierGenerator
to generate composite keys. Instead the application must assign its own identifiers.
Use the
<composite-id>
tag, with nested <key-property>
elements, in place of the usual <id>
declaration. For example, the OrderLine
class has a primary key that depends upon the (composite) primary key of Order
.
Any foreign keys referencing the
OrderLine
table are now composite. Declare this in your mappings for other classes. An association to OrderLine
is mapped like this:
Note
The
<column>
tag is an alternative to the column
attribute everywhere.
A
many-to-many
association to OrderLine
also uses the composite foreign key:
The collection of
OrderLine
s in Order
would use:
The
<one-to-many>
element declares no columns.
If
OrderLine
itself owns a collection, it also has a composite foreign key.