Mapping Set using @ElementCollection

前端 未结 3 1419
面向向阳花
面向向阳花 2020-12-02 23:29

I have the following enum:

package ir.raysis.tcs.rule.days;

public enum Days {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY;
}


        
相关标签:
3条回答
  • 2020-12-02 23:55

    Try using @CollectionTable and not @JoinTable

    0 讨论(0)
  • 2020-12-02 23:57

    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

    0 讨论(0)
  • 2020-12-03 00:06

    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.

    0 讨论(0)
提交回复
热议问题