Issue
I have simple spring boot application, i decided to add spring security there and roles. Here's my user class:
//different fields
@ElementCollection(targetClass = UserRole.class, fetch = FetchType.EAGER)
@CollectionTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id"))
@Enumerated(EnumType.STRING)
private Set<UserRole> userRoles;
and the UserRole
enum:
public enum UserRole implements GrantedAuthority {
USER, ADMIN;
@Override
public String getAuthority() {
return name();
}
}
I have 2 tables
users:
| id | name | pasword |
----------------------
| | | |
user_roles:
| user_id | user_role |
-----------------------
| | |
When i am trying to create new user i get:
Unknown column 'user_roles' in 'field list'
I throught it should add new record also to user_roles.
Solution
Try adding an @Column
to specify the column name of the userRoles
so that it matches the name of the column in the user_roles
collection table:
@ElementCollection(targetClass = UserRole.class, fetch = FetchType.EAGER)
@CollectionTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id"))
@Enumerated(EnumType.STRING)
@Column(name = "user_role")
private Set<UserRole> userRoles;
The user_roles
name that is shown in the error is probably the default column name that Hibernate tries to look for according to the default naming strategy, when the @Column
is not specified.
Answered By - M A