我尝试通过User
实体加载@ManyToMany
实体的用户角色进行身份验证,但我得到了异常。
ERROR: column user0_.role_id does not exist
我刚开始学习弹簧数据。请帮我解决这个问题。谢谢。
用户实体:
@Entity(name = "users")
public class User implements UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
@Column(name = "role_id")
private int roleId;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "user_id_role_id"
,
joinColumns = {
@JoinColumn(name = "user_id",
nullable = false,
updatable = false)
},
inverseJoinColumns = {
@JoinColumn(name = "role_id",
nullable = false,
updatable = false)
}
)
private List<Role> authorities;
@Column(name = "account_non_expired")
private boolean accountNonExpired;
@Column(name = "account_non_locked")
private boolean accountNonLocked;
@Column(name = "credentials_non_expired")
private boolean credentialsNonExpired;
@Column(name = "enabled")
private boolean enabled;
...
和角色实体:
@Entity(name = "user_role")
public class Role implements GrantedAuthority {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "role")
private String role;
...
我的PostgreSQL架构:
CREATE TABLE IF NOT EXISTS user_role (
id SERIAL PRIMARY KEY,
role VARCHAR(10) NOT NULL
);
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
username VARCHAR(20) UNIQUE NOT NULL,
password VARCHAR(20) UNIQUE NOT NULL,
account_non_expired BOOLEAN NOT NULL DEFAULT TRUE,
account_non_locked BOOLEAN NOT NULL DEFAULT TRUE,
credentials_non_expired BOOLEAN NOT NULL DEFAULT TRUE,
enabled BOOLEAN NOT NULL DEFAULT TRUE
);
CREATE TABLE user_id_role_id (
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL,
role_id INTEGER NOT NULL,
FOREIGN KEY (role_id) REFERENCES user_role (id),
FOREIGN KEY (user_id) REFERENCES users (id)
);
答案 0 :(得分:2)
错误表示您在表用户中没有属性role_id。 您在实体中拥有此属性:
@Column(name = "role_id")
private int roleId;
但是当你创建表时:
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
username VARCHAR(20) UNIQUE NOT NULL,
password VARCHAR(20) UNIQUE NOT NULL,
account_non_expired BOOLEAN NOT NULL DEFAULT TRUE,
account_non_locked BOOLEAN NOT NULL DEFAULT TRUE,
credentials_non_expired BOOLEAN NOT NULL DEFAULT TRUE,
enabled BOOLEAN NOT NULL DEFAULT TRUE
);
你没有属性role_id。
只需从User实体中删除属性roleId,它应该可以工作:)