Substituting restrictions on collection of dependent objects in sleep mode

The hibernate documentation for dependent collections states that:

You cannot reference a purchase on the other side for a bi-directional navigation link. Components are value types and do not allow shared references. One purchase can be included in the Order, but it cannot be referenced at the same time.

Can someone please help me understand this?

1) Why hibernation restricts the buy link to the other side?

2) Why doesn't this allow for generic links?

3) What does it mean that One Purchase cannot be listed in the clause at the same time.?

Can someone please explain this with some examples.

+3


source to share


1 answer


To explain this, I would start with another example from the doc:

The code snippet shows the display of a collection of strings:

<set name="aliases"
            table="person_aliases" 
            sort="natural">
    <key column="person"/>
    <element column="name" type="string"/>
</set>

      

In this case, we have a set represented as rendered with . aliases

List<string>

<element>

We can clearly see that every element here (alias) is a string

- Value Type (as opposed to a reference type). We also did not expect that there might be some other place in the system referencing this element ...
because it is not a type of summarization .

Now let's move on to:



What we see is an example that is (very) similar, but <element>

used instead <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 we have a related object in Java (string) - for the above construction we need a special type. And it will string

class Pruchase {}

But even if it is a custom type - our own class, in this scenario it is represented as a Value Type (again, just like the opposite of the Reference type).

Why? Because it has no identifier, any key is for a link. It is built from a domain perspective. Maybe this quote from this doc can help more:

Like value types, components do not support shared references. In other words, two people can have the same name, but two human objects will contain two independent name objects that are only "the same" in meaning.

Finally:

This is a function. The fact that we can use <composite-element>

and <element>

does not mean that we should. We can still convert the Purchase class to a first-level citizen by mapping it as <class>

. Then all the standard things will work again - because it will be a Reference Type ...

+1


source







All Articles