The hibernate documentation for dependent collections says that:
There cannot be a reference to the purchase on the other side for bidirectional ass
To explain that, I would start with another example from doc:
Code snippet shows the mapping of collection of strings:
<set name="aliases"
table="person_aliases"
sort="natural">
<key column="person"/>
<element column="name" type="string"/>
</set>
In this case, we do have a collection of aliases
, represented as a List<string>
, mapped with an <element>
.
We can clearly see, that each element here (alias) is a string
- Value Type (in the meaning of the opposite to the Reference type). We also would not expect, that there could be any further place in the system, referencing this element...
because it is not a Refeence type.
Now, let's move to the:
What we see is an example, (very) similar, but instead of <element>
, it is using the <composite-element>
:
<set name="purchasedItems" table="purchase_items" lazy="true">
<key column="order_id">
<composite-element class="eg.Purchase">
<property name="purchaseDate"/>
<property name="price"/>
<property name="quantity"/>
<many-to-one name="item" class="eg.Item"/> <!-- class attribute is optional -->
</composite-element>
</set>
While for string
we have related object in Java (string) - for the above construct, we do need custom type. And that would be a class Pruchase {}
But even if this is a custom type - our own class, in this scenario it is represented as a Value Type (again, as opposite to Reference type).
Why? Because it does not have any id, any key - to be referenced. It is construct from Domain modeling point of view. Maybe this cite from that doc could help more:
Like value types, components do not support shared references. In other words, two persons could have the same name, but the two person objects would contain two independent name objects that were only "the same" by value.
Finally:
This is a feature. The fact we can use <composite-element>
and <element>
, does not mean, that we must. We still can convert the Purchase class into first level citizen, by mapping it as a <class>
. Then all standard stuff will work again - because it would represent a Reference Type...