java 如何使用 Hibernate JPA 注释映射嵌套集合 Map<Key,List<Values>>?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/939235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:27:37  来源:igfitidea点击:

How do I map a nested collection, Map<Key,List<Values>>, with hibernate JPA annotations?

javahibernatecollectionsjpaannotations

提问by Nathan Feger

I have a class I am not sure how to annotate properly.

我有一堂课,我不确定如何正确注释。

My goal for Holder::data:

我的 Holder::data 目标:

  • List should maintain order not by comparator but by the natural ordering of the elements in the array. (Which can be an ndx column if that is helpful.)
  • Holder will have the only reference to data, so Cascade all is probably applicable as well.
  • 列表应该不是通过比较器而是通过数组中元素的自然顺序来维护顺序。(如果有帮助,它可以是 ndx 列。)
  • Holder 将拥有对数据的唯一引用,因此 Cascade all 可能也适用。

I am also open to a different design that removes the map, if that would make for a cleaner design.

我也对删除地图的不同设计持开放态度,如果这可以使设计更简洁。

@Entity
public class Holder extends DomainObject {
  private Map<Enum,List<Element>> data;
}

@Entity
public class Element extends DomainObject {
  private long valueId;
  private int otherData;
}

@Mappedsuperclass
public class DomainObject {
 // provides id
 // optimistic locking
 // create and update date
}

回答by Maarten Winkels

I don't think it is possible with hibernate(-core) to map any collection of collections:

我认为 hibernate(-core) 不可能映射任何集合集合:

Collections may contain almost any other Hibernate type, including all basic types, custom types, components, and of course, references to other entities.

集合几乎可以包含任何其他 Hibernate 类型,包括所有基本类型、自定义类型、组件,当然还有对其他实体的引用。

(from the official doc)

(来自官方文档

Notice the almostand the omission of the collection type.

注意集合类型的几乎和省略。

A workaround: You need to introduce a new type 'in between' the collection holder and the element. This type you can map as an entity or a component and it refers the original content of the map, in this case a list.

解决方法:您需要在集合持有者和元素之间“中间”引入一种新类型。您可以将此类型映射为实体或组件,它引用映射的原始内容,在本例中为列表。

Something like:

就像是:

@Entity
public class Holder extends DomainObject {
  @OneToMany
  private Map<Enum,InBetween> inBetweens;
}

@Entity
public class InBetween extends DomainObject {
  @OneToMany
  private List<Element> elements;
}

@Entity
public class Element extends DomainObject {
  private long valueId;
  private int otherData;
}

@Mappedsuperclass
public class DomainObject {
 // provides id
 // optimistic locking
 // create and update date
}

The rest of the mapping depends on your particular situation, but is rather straightforward.

映射的其余部分取决于您的特定情况,但相当简单。

回答by Anton

Here is a blog about collection of collections in hibernate http://blog.xebia.com/2007/10/05/mapping-multimaps-with-hibernate/

这是一个关于 hibernate 集合集合的博客http://blog.xebia.com/2007/10/05/mapping-multimaps-with-hibernate/

Hope it will help. It helped me.

希望它会有所帮助。它帮助了我。

Regards, Anton

问候, 安东

回答by ngeek

Please note that the referred link to the Hibernate documentation seems out of date, I found the following working: http://docs.jboss.org/hibernate/core/3.5/reference/en/html/collections.html

请注意 Hibernate 文档的引用链接似乎已过时,我发现以下工作正常:http: //docs.jboss.org/hibernate/core/3.5/reference/en/html/collections.html