Mapping Set<enum> using @ElementCollection

for future googlers! finally i managed to solve the problem, i just had to put the annotations somewhere else in my code ,

@ElementCollection(targetClass = Days.class)
@CollectionTable(name = "days", joinColumns = @JoinColumn(name = "rule_id"))
@Column(name = "daysOfWeek", nullable = false)
@Enumerated(EnumType.STRING)
public Set<Days> getDays() {
    return days;
}

as you can see i wrote the annotation code above and before the getter method(instead of placing it before the attribute declaration code) and problem solved, anyone who can explain me what causes this will be appreciated. thank you


Try using @CollectionTable and not @JoinTable


While the other question is correct, the most simple form could be:

@ElementCollection
@Enumerated
private Set<EnumName> enumValues;

everything else would be set by convention over configuration (join-table-name, columns).

I highly recommend using @Enumerated(EnumType.STRING) - look it up why. And you might need @ElementCollection(fetch = FetchType.EAGER) depending on what (and where) you're doing.