I'm confused about when to use one over the other, or both together.
From the answers to this question, I get that @ElementCollection
is used when we want to map a non-entity. But then there seems to be cases when both are used at the same time, as here,
@ElementCollection(targetClass=Role.class)
@JoinTable(name = "USER_ROLES", joinColumns = @JoinColumn(name = "USER_ID"))
@Enumerated(EnumType.STRING)
@Column(name = "role", nullable = false)
public Set<Role> getRoles() {
return roles;
}
or here,
@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;
}
But then, I don't really understand when they shouldn't go together. I guess, but I'm not sure, that the question is whether my enum is an entity or not, as said in this answer.
You should decide whether your Platform is an entity or not.
So my questions are:
Do
@ElementCollection
and@Enumerated
overlap in any way or are completely independent? If independent, I decide if my entity is mapped, and then if it happens to be an enum. Correct?How do I decide if I want my enum to be an entity? An example would be highly appreciated.