JPA 中有没有办法映射实体类中的枚举集合?或者唯一的解决方案是用另一个域类包装 Enum 并使用它来映射集合?
@Entity
public class Person {
public enum InterestsEnum {Books, Sport, etc... }
//@???
Collection<InterestsEnum> interests;
}
我正在使用 Hibernate JPA 实现,但当然更喜欢实现不可知的解决方案。
JPA 中有没有办法映射实体类中的枚举集合?或者唯一的解决方案是用另一个域类包装 Enum 并使用它来映射集合?
@Entity
public class Person {
public enum InterestsEnum {Books, Sport, etc... }
//@???
Collection<InterestsEnum> interests;
}
我正在使用 Hibernate JPA 实现,但当然更喜欢实现不可知的解决方案。
Andy 的回答中的链接是在 JPA 2 中映射“非实体”对象集合的一个很好的起点,但在映射枚举时并不完整。这是我想出来的。
@Entity
public class Person {
@ElementCollection(targetClass=InterestsEnum.class)
@Enumerated(EnumType.STRING) // Possibly optional (I'm not sure) but defaults to ORDINAL.
@CollectionTable(name="person_interest")
@Column(name="interest") // Column name in person_interest
Collection<InterestsEnum> interests;
}
我能够以这种简单的方式完成此操作:
@ElementCollection(fetch = FetchType.EAGER)
Collection<InterestsEnum> interests;
预先加载需要,以避免延迟加载inizializing错误的解释在这里。
tl;dr 一个简短的解决方案如下:
@ElementCollection(targetClass = InterestsEnum.class)
@CollectionTable
@Enumerated(EnumType.STRING)
Collection<InterestsEnum> interests;
长的答案是,使用此注释,JPA 将创建一个表,该表将保存指向主类标识符(在本例中为 Person.class)的 InterestsEnum 列表。
@ElementCollections 指定 JPA 可以在何处找到有关 Enum 的信息
@CollectionTable 创建保存从 Person 到 InterestsEnum 的关系的表
@Enumerated(EnumType.STRING) 告诉 JPA 将枚举作为字符串持久化,可以是 EnumType.ORDINAL
我正在使用对 java.util.RegularEnumSet 的轻微修改来获得持久的 EnumSet:
@MappedSuperclass
@Access(AccessType.FIELD)
public class PersistentEnumSet<E extends Enum<E>>
extends AbstractSet<E> {
private long elements;
@Transient
private final Class<E> elementType;
@Transient
private final E[] universe;
public PersistentEnumSet(final Class<E> elementType) {
this.elementType = elementType;
try {
this.universe = (E[]) elementType.getMethod("values").invoke(null);
} catch (final ReflectiveOperationException e) {
throw new IllegalArgumentException("Not an enum type: " + elementType, e);
}
if (this.universe.length > 64) {
throw new IllegalArgumentException("More than 64 enum elements are not allowed");
}
}
// Copy everything else from java.util.RegularEnumSet
// ...
}
这个类现在是我所有枚举集的基础:
@Embeddable
public class InterestsSet extends PersistentEnumSet<InterestsEnum> {
public InterestsSet() {
super(InterestsEnum.class);
}
}
我可以在我的实体中使用该集合:
@Entity
public class MyEntity {
// ...
@Embedded
@AttributeOverride(name="elements", column=@Column(name="interests"))
private InterestsSet interests = new InterestsSet();
}
好处:
java.util.EnumSet
有关说明)缺点:
RegularEnumSet
并且PersistentEnumSet
几乎相同)
EnumSet.noneOf(enumType)
在您的PersistenEnumSet
,声明AccessType.PROPERTY
并提供两种使用反射来读取和写入elements
字段的访问方法@Embeddable
到PersistentEnumSet
并删除多余的类(... interests = new PersistentEnumSet<>(InterestsEnum.class);
)@AttributeOverride
如果PersistentEnumSet
实体中有多个,则必须使用,如我的示例中所示(否则两者都将存储到同一列“元素”中)values()
构造函数中的with 反射访问不是最佳的(尤其是在查看性能时),但其他两个选项也有其缺点:
EnumSet.getUniverse()
实现使用了一个sun.misc
类JPA 中的集合是指一对多或多对多的关系,它们只能包含其他实体。抱歉,您需要将这些枚举包装在一个实体中。如果您考虑一下,无论如何您都需要某种 ID 字段和外键来存储此信息。除非你做一些疯狂的事情,比如在字符串中存储一个逗号分隔的列表(不要这样做!)。
使用 Hibernate 你可以做
@CollectionOfElements(targetElement = InterestsEnum.class) @JoinTable(name = "tblInterests", joinColumns = @JoinColumn(name = "personID")) @Column(name = "interest", nullable = false) @Enumerated(EnumType.STRING) Collection<InterestsEnum> interests;