我需要保留类型为 Map<Item, Set<Item>>
的成员,使用 JPA2 注释。
关系是多对多的,Item 对象是实体。
我是否应该创建一个单独的中介实体持有 Set<Item>
还是可以直接映射?
如果缺少某些上下文,请告知。
最佳答案
Should I create a separate intermediary Entity holding
Set<Item>
or is a direct mapping possible?
JPA 不支持嵌套集合关系(List
of List
s,Map
of Set
s,等等)。这是关于 Map
的规范的相关部分:
2.7 Map Collections
Collections of elements and entity relationships can be represented as java.util.Map collections.
The map key and the map value independently can each be a basic type, an embeddable class, or an entity.
...
所以是的,使用持有 Set<Item>
的实体然后将您的关系映射为 Map<Item, MyHolder>
.
引用资料
- JPA 2.0 规范
- 第 2.7 节“ map 集”
- JPA 维基书籍
关于java - 包含 Set 的 @ManyToMany Map 的 JPA2 持久性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4125349/