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

标签 java hibernate

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

所以这里说复合元素可以包含组件,但不能包含集合。这与上面的说法相矛盾,有人可以解释一下组件是否可以包含集合。如果可能,请提供一个小例子。

最佳答案

任何复合元素都不能包含集合。但组件可以。让我试着这样解释一下:

  • 第一个引用是关于 <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/

相关文章:

java - 无法向spring boot jpa的@Query注解写入查询

java - 如何检查一个区间是否包含2的幂

java - 如何在java中以编程方式从任何句子的中间获取输入?

java - 如何使用Aspose.Word for Java在word文件中设置水印

java - 如何绑定(bind) Activity 中 NavigationView 中的 TextView

java - org.apache.axis2.AxisFault : Mapping qname not fond for the package: org. hibernate.collection

java - 单击 JTable 中我的图标的操作

java - 说 JAXB 之于 XML 就像 Hibernate 之于数据库模式一样准确吗?

java - 错误-日期在Jparepository中不起作用

java - 我如何使用 thymeleaf 以一种形式显示 2 个实体的值