Hibernate - 复合值集

时间:2011-03-22 15:03:58

标签: java hibernate

我需要在Hibernate中使用Collection个复合值。类似的东西:

class Parent {
    Set<Child> children;
}

class Child {
    String property;
    String anotherProperty;
    MyOtherClass oneToOneClass;
}

关键要求是此集合中的元素是值对象。当我saveOrUpdate Parent时,它也会保存其子女。

更重要的是,当我根据相同的集合创建另一个Parent children时,这些孩子需要单独保留。这就是常规one-to-many对我不起作用的原因。

我可以用Hibernate做一个干净的方法吗?像这里描述的值集合:http://docs.jboss.org/hibernate/core/3.3/reference/en/html/collections.html - 但对于具体的复合类。

我更喜欢XML而不是注释的解决方案。

1 个答案:

答案 0 :(得分:0)

通常将其映射为复合元素

<set ... >
  <key .../>
  <composite-element class="Child" ...>
    <property name="property"/>
    <property name="anotherProperty"/>
    <nested-composite-element name="oneToOneClass">
      <property name="..."/>
    </nested-composite-element>
</set>

请参阅Component Mapping

相关问题