在 JPA 中有没有办法在 Entity 类中映射一组枚举?或者唯一的解决方案是用另一个域类包装 Enum 并使用它来映射集合?
@Entity
public class Person {
public enum InterestsEnum {Books, Sport, etc... }
//@???
Collection<InterestsEnum> interests;
}
我正在使用 Hibernate JPA 实现,但当然更喜欢实现无关的解决方案。
最佳答案
使用 Hibernate 你可以做到
@ElementCollection(targetElement = InterestsEnum.class)
@JoinTable(name = "tblInterests", joinColumns = @JoinColumn(name = "personID"))
@Column(name = "interest", nullable = false)
@Enumerated(EnumType.STRING)
Collection<InterestsEnum> interests;
关于java - 枚举的 JPA 映射集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/416208/