问题描述
我有一门课,我不知道如何正确注释.
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<Key,List<Values>>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!