JPA map collection of Enums

后端 未结 6 704
滥情空心
滥情空心 2020-11-27 12:46

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?<

6条回答
  •  我在风中等你
    2020-11-27 13:20

    The link in Andy's answer is a great starting point for mapping collections of "non-Entity" objects in JPA 2, but isn't quite complete when it comes to mapping enums. Here is what I came up with instead.

    @Entity
    public class Person {
        @ElementCollection(targetClass=InterestsEnum.class)
        @Enumerated(EnumType.STRING) // Possibly optional (I'm not sure) but defaults to ORDINAL.
        @CollectionTable(name="person_interest")
        @Column(name="interest") // Column name in person_interest
        Collection interests;
    }
    

提交回复
热议问题