I have the following two fields in a Hibernate managed entity:
@ElementCollection(targetClass = PersonAttributes.class, fetch = FetchType.EAGER)
@Enumerated(EnumType.STRING)
@CollectionTable(name = "PERSON_ATTRIBUTES", joinColumns = { @JoinColumn(name = "PERSON_ID") })
@Column(name = "ATTRIBUTES")
private List<PersonAttributes> attributes;
@ElementCollection(targetClass = PersonRoles.class, fetch = FetchType.EAGER)
@Enumerated(EnumType.STRING)
@CollectionTable(name = "PERSON_ROLES", joinColumns = { @JoinColumn(name = "PERSON_ID") })
@Column(name = "ROLES")
private List<PersonRoles> roles;
When I choose two PersonAttributes
values and two PersonRoles
values and persist the entity, on reading it from the database, the values are cross-multiplized.
Example:
Persisting roles=[ADMIN, SUPERVISOR]
and attributes=[ATTRIBUTE1, ATTRIBUTE2]
. On reading the entity from Database I get roles=[ADMIN, SUPERVISOR, ADMIN, SUPERVISOR]
and attributes=[ATTRIBUTE1, ATTRIBUTE1, ATTRIBUTE2, ATTRIBUTE2]
.
Another Example:
Persisting roles=[ADMIN]
and attributes=[ATTRIBUTE1, ATTRIBUTE2]
.
On reading the entity from Database I get roles=[ADMIN, ADMIN]
and attributes=[ATTRIBUTE1, ATTRIBUTE2]
.
Looks there is some unexpected joining? How can I get Hibernate reading the persisted enumerations without this cross-multiplication?
Additional information: This occurs when I use the entityManager.find()
method. But when I get all entities as list via JPQL SELECT Statement, the enumerations are appropriate.