gpt4 book ai didi

java - 复合元素可以包含 Hibernate 中的集合吗

转载 作者:行者123 更新时间:2023-11-30 09:03:24 25 4
gpt4 key购买 nike

根据 hibernate documentation ,它说:

The properties of a component can be of any Hibernate type (collections, many-to-one associations, other components, etc). Nested components should not be considered an exotic usage. Hibernate is intended to support a fine-grained object model.

通过这条语句,我明白我可以声明一个组件,其属性可以是集合。

现在文档又说了:

Composite elements can contain components but not collections. If your composite element contains components, use the 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.

所以这里说Composite elements can contain components but not collections.这与上面的说法相矛盾,谁能解释一下一个组件是否可以有一个集合。如果可能,请提供一个小示例。

最佳答案

没有复合元素不能包含集合。但是组件可以。让我试着这样解释:

  • 第一个引用是关于 <component>元素,同时
  • 第二个是关于 <composite-element>

它们都允许我们映射值类型对象(与引用类型相反) - 正如这里详细讨论的那样:

那么,区别在哪里呢?为什么第一个( <component> ) 可以包含集合,而第二个( <composite-element> ) 不能?简化的答案是:

Because the <composite-element> is mapping of the collection item already.

<component>另一方面,是根上的映射 - <class>等级。

完整且更好的答案是:

both types do not have their ID, their KEY in relational DB structure. That's why they are treated as Value Types - they cannot be referenced.

  • The <component> is in relation one-to-one with its root <class> - it has access to the root/class ID. Therefore it could path to collection <key column=""> the root id.
  • The <composite-element> does not have access to its ID/Key (does not exist at all) and is not on the same level as <component>. There is no place to cheat as we did with component

所以,虽然我们可以看到这样的映射

<class name="eg.Person" table="person">
<id ...
...
<component name="Name" class="eg.Name" unique="true">
<parent name="namedPerson"/> //reference back to the Person
<property name="initial"/>
<property name="first"/>
<property name="last"/>

// here we go
// collection mapped inside of the <component>

<set name="celebrationDates" table="name_Dates">
<key column="person"/>
<element column="name" type="date"/>
</set>
</component>
</class>

<composite-element>我们最终会得到一个集合项映射:

<class name="eg.Order" .... >
....
<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"/>

// this is already component
// but without ID to be used
// as a reference on the other side of bidirectional relation

</composite-element>
</set>
</class>

总结,两种映射都试图提供相似的功能。它们确实有不同的名称,因为它们用于不同的场景。

我们可以想象 <composite-element> 的集合可以工作,如果我们 ...并停在这里。如果我们需要,请不要使用组件。使用完整 <class>映射。

关于java - 复合元素可以包含 Hibernate 中的集合吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25636708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com