Kadzhaev Marat Kadzhaev Marat -4 years ago 149
Java Question

UserDetails and hibernate exception

I am trying to start my web application, but I get exception:

java.util.Collection, at table: user, for columns: [org.hibernate.mapping.Column(authorities)]

I am sorry for agglutinate code, but SO requires less code and more details.

It is my User entity:

@Table(name = "user")
public class User extends BaseEntity implements UserDetails {
private String email;
private String password;
private Set<Role> roles;
@Column(name = "email", nullable = false, unique = true)
public String getEmail() {return email;}
public void setEmail(String email) {this.email = email;}
@Column(name = "password", nullable = false)
public String getPassword() {return password;}
public void setPassword(String password) {this.password = password;}
@JoinTable(name = "user_role",
joinColumns = {@JoinColumn(name = "user", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "role", referencedColumnName = "name")})
public Set<Role> getRoles() { return roles; }
public void setRoles(Set<Role> role) {this.roles = role;}
public Collection<? extends GrantedAuthority> getAuthorities() {return roles;}
public String getUsername() { return email; }
public boolean isAccountNonExpired() {return true; }
public boolean isAccountNonLocked() { return true; }
public boolean isCredentialsNonExpired() { return true; }
public boolean isEnabled() { return true; }

And it is the Role entity:

@Table(name = "Role")
public class Role implements GrantedAuthority, Serializable {
private String name;
@Column(name = "name")
public String getName() {return name;}
public void setName(String name) { this.name = name; }
public String getAuthority() {return name;}

Answer Source

I found the cause for this problem. If we put annotation above getters then Hibernate automatically trying create column for other getters which are not annotated. There are two variants:

1) Move all annotations above fields.

2) Add @Transient to all getters which are not a columns.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download