枚举的 JPA 地图集合 [英] JPA map collection of Enums

查看:28
本文介绍了枚举的 JPA 地图集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 JPA 中有没有办法映射实体类中的枚举集合?或者唯一的解决办法是用另一个域类包装 Enum 并用它来映射集合?

Is there a way in JPA to map a collection of Enums within the Entity class? Or the only solution is to wrap Enum with another domain class and use it to map the collection?

@Entity
public class Person {
    public enum InterestsEnum {Books, Sport, etc...  }
    //@???
    Collection<InterestsEnum> interests;
}

我正在使用 Hibernate JPA 实现,但当然更喜欢与实现无关的解决方案.

I am using Hibernate JPA implementation, but of course would prefer implementation agnostic solution.

推荐答案

使用 Hibernate 你可以做到

using Hibernate you can do

@CollectionOfElements(targetElement = InterestsEnum.class)
@JoinTable(name = "tblInterests", joinColumns = @JoinColumn(name = "personID"))
@Column(name = "interest", nullable = false)
@Enumerated(EnumType.STRING)
Collection<InterestsEnum> interests;

这篇关于枚举的 JPA 地图集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆