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

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

问题描述

JPA中是否有方法在Entity类中映射枚举的集合?或者唯一的解决方案是将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天全站免登陆