Kadzhaev Marat Kadzhaev Marat - 28 days ago 19
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:

@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;}
@ManyToMany
@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;}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {return roles;}
@Override
public String getUsername() { return email; }
@Override
public boolean isAccountNonExpired() {return true; }
@Override
public boolean isAccountNonLocked() { return true; }
@Override
public boolean isCredentialsNonExpired() { return true; }
@Override
public boolean isEnabled() { return true; }
}


And it is the Role entity:

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

Answer

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.