gpt4 book ai didi

java - 枚举的 JPA 映射集合

转载 作者:IT老高 更新时间:2023-10-28 11:45:35 24 4
gpt4 key购买 nike

在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com