1

I have User with roles in JPA (hibernate). If I remove role from set on user it's not propagate to database. So its still holded in the database. (I also noticed that this happend only sometimes)
If I set the role set to null persist than set only the right role to the user set and persist again than its work.

public enum Role {
        USER("USER"),
        SPECIALUSER("SPECIALUSER"),
        ADMIN("ADMIN");

        private final String name;

        private Role(final String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return name;
        }    

Declaration in User entity

@ElementCollection(targetClass = Role.class, fetch = FetchType.EAGER)
    @Enumerated(EnumType.STRING)
    @CollectionTable(name = "USER_ROLES")
    @Column(name = "ROLE")
    private Set<Role> roles;

in transaction on managed entity

user.getRoles().remove(Role.ADMIN);
user.getRoles().remove(Role.SPECIALUSER);

I also try to flush but its not helped. I am quite confused...

Milkmaid
  • 1,659
  • 4
  • 26
  • 39

1 Answers1

0

Try adding an @OrderColumn as well:

@ElementCollection(targetClass = Role.class, fetch = FetchType.EAGER)
@Enumerated(EnumType.STRING)
@CollectionTable(name = "USER_ROLES")
@Column(name = "ROLE")
@OrderColumn(name = "index_id")
private Set<Role> roles;
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911