此内容没有您所选择的语言版本。
8.3. Collections of Dependent Objects
Collections of components are supported (e.g. an array of type
Name
). Declare your component collection by replacing the <element>
tag with a <composite-element>
tag:
Important
If you define a
Set
of composite elements, it is important to implement equals()
and hashCode()
correctly.
Composite elements can contain components but not collections. If your composite element contains components, use the
<nested-composite-element>
tag. This case is a collection of components which themselves have components. You may want to consider if a one-to-many association is more appropriate. Remodel the composite element as an entity, but be aware that even though the Java model is the same, the relational model and persistence semantics are still slightly different.
A composite element mapping does not support null-able properties if you are using a
<set>
. There is no separate primary key column in the composite element table. Hibernate uses each column's value to identify a record when deleting objects, which is not possible with null values. You have to either use only not-null properties in a composite-element or choose a <list>
, <map>
, <bag>
or <idbag>
.
A special case of a composite element is a composite element with a nested
<many-to-one>
element. This mapping allows you to map extra columns of a many-to-many association table to the composite element class. The following is a many-to-many association from Order
to Item
, where purchaseDate
, price
and quantity
are properties of the association:
There cannot be a reference to the purchase on the other side for bidirectional association navigation. Components are value types and do not allow shared references. A single
Purchase
can be in the set of an Order
, but it cannot be referenced by the Item
at the same time.
Even ternary (or quaternary, etc) associations are possible:
Composite elements can appear in queries using the same syntax as associations to other entities.