Search code examples
javajpajakarta-ee

JPA map collection of Enums


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;
}

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


Solution

  • using Hibernate you can do

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