Nov 1, 2018

JPA Enum Collection Example

How to Persist set of Enums in a many-to-many JPA mapping

Example:

Account Entity which maps to User table, primary key: userid.

Table account_standard_attribute has foreign key userid points to User table (Account entity). attribute column should be Enumerated by enum StandardAttribute class.

In Account Entity:
    @ElementCollection(targetClass = StandardAttribute.class)
    @CollectionTable(name="account_standard_attribute", joinColumns= @JoinColumn(name="userid"))
    @Column(name="attribute")
    @Enumerated(EnumType.STRING)
    private List<StandardAttribute> standardAttributes;