解决在hibernate中收集依赖对象的限制 [英] Understading the restrictions for collection of dependent objects in hibernate

查看:90
本文介绍了解决在hibernate中收集依赖对象的限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hibernate文档依赖集合表示:


对于
双向关联的另一方,导航。组件是值类型,
不允许共享引用。一个Purchase可以在
的一个Order中,但它不能被Item同时引用。



<有人可以帮助我理解这一点吗?

<1>为什么hibernate会限制购买的参考?

2)为什么它不允许共享引用?

3)这意味着一件购物不能同时被商品引用。



有人可以用一些例子来解释一下。

解释说,我会从doc的另一个例子开始:



代码片段显示字符串集合的映射:

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

在这种情况下,我们有一个 别名 ,表示为< string> ,映射到< element>



我们可以清楚地看到,这里的每个元素<别名> 都是字符串 - 值类型(与引用类型相反)。我们也不希望系统中可能会有更多的地方,引用这个元素...

,因为它不是 a Refeence type



现在,我们来看看:



我们看到的是一个例子,(very)但不是< element> ,而是使用< composite-element>

 < set name =purchasedItemstable =purchase_itemslazy =true> 
< key column =order_id>
< composite-element class =eg.Purchase>
< property name =purchaseDate/>
< property name =price/>
< property name =quantity/>
< / composite-element>
< / set>

对于 字符串 (string) - 对于上面的构造,我们确实需要自定义类型。这就是 class Pruchase {}



但即使这是一种自定义类型 - 我们自己的类,在这种情况下,它被表示为一个值类型(同样,与引用类型相反)

为什么?因为它没有任何id,所以要引用任何键。它是从领域建模的角度来构建的。也许这是从 doc 可以提供更多帮助:


与值类型相似,组件不支持共享引用。换句话说,两个人可以具有相同的名称,但是两个人物对象将包含两个独立的名称对象,这些名称对象的值仅为相同。

最后:

这是一项功能。我们可以使用< composite-element> < element> 的事实并不意味着我们必须。我们仍然可以将 Purchase 类转换为一级公民,方法是将其映射为< class> 。然后所有标准的东西都会再次运行 - 因为它会代表引用类型 ...

...

The hibernate documentation for dependent collections says that:

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.

Can someone please help me in understanding this?

1) Why hibernate restricts the reference of purchase on other side?

2) Why it doesn't allow shared references?

3) What does it mean that a single purchase cannot be referenced by the Item at the same time.?

Can someone please explain this with some examples.

解决方案

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...

这篇关于解决在hibernate中收集依赖对象的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆