如何使用休眠 JPA 注释映射嵌套集合 Map<Key,List<Values&gt

How do I map a nested collection, Maplt;Key,Listlt;Valuesgt;gt;, with hibernate JPA annotations?(如何使用休眠 JPA 注释映射嵌套集合 Maplt;Key,Listlt;Valuesgt;gt;?)
本文介绍了如何使用休眠 JPA 注释映射嵌套集合 Map<Key,List<Values>>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一门课,我不知道如何正确注释.

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

我对 Holder::data 的目标:

My goal for Holder::data:

  • 列表不应该通过比较器而是通过数组中元素的自然顺序来保持顺序.(如果有帮助,它可以是一个 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
}

推荐答案

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

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

集合几乎可以包含任何其他 Hibernate 类型,包括所有基本类型、自定义类型、组件、当然,参考其他实体.

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

(来自官方文档)

注意几乎和集合类型的遗漏.

Notice the almost and 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.

类似:

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

这篇关于如何使用休眠 JPA 注释映射嵌套集合 Map&lt;Key,List&lt;Values&gt;&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

quot;Char cannot be dereferencedquot; error(“Char 不能被取消引用错误)
Java Switch Statement - Is quot;orquot;/quot;andquot; possible?(Java Switch 语句 - 是“或/“和可能的?)
Java Replace Character At Specific Position Of String?(Java替换字符串特定位置的字符?)
What is the type of a ternary expression with int and char operands?(具有 int 和 char 操作数的三元表达式的类型是什么?)
Read a text file and store every single character occurrence(读取文本文件并存储出现的每个字符)
Why do I need to explicitly cast char primitives on byte and short?(为什么我需要在 byte 和 short 上显式转换 char 原语?)