试图实现 Spring Security 并且无法映射角色和用户。所以我有“users”、“roles”和“user_role_connector”数据库表:
CREATE TABLE users (
user_pk INT NOT NULL AUTO_INCREMENT,
...
PRIMARY KEY (user_pk));
CREATE TABLE roles (
role_pk INT NOT NULL AUTO_INCREMENT,
role VARCHAR(20) NOT NULL UNIQUE,
PRIMARY KEY (role_pk));
CREATE TABLE user_role_connector (
urc_pk INT NOT NULL AUTO_INCREMENT,
user_fk INT NOT NULL,
role_fk INT NOT NULL,
FOREIGN KEY (user_fk) REFERENCES users(user_pk),
FOREIGN KEY (role_fk) REFERENCES roles(role_pk),
PRIMARY KEY (urc_pk));
我有角色和用户的实体类。两者都可以工作,直到我添加映射:
@Entity
@Table(name = "users")
public final class UserAccount {
private int user_pk;
...
@OneToOne(cascade=CascadeType.ALL)
@JoinTable(name = "user_role_connector",
joinColumns = {@JoinColumn(name = "user_fk", referencedColumnName = "user_pk")},
inverseJoinColumns = {@JoinColumn(name = "role_fk", referencedColumnName = "role_pk")}
)
private Role role;
@Entity
@Table(name = "roles")
final public class Role {
private int role_pk;
private String role;
@OneToMany(cascade=CascadeType.ALL)
@JoinTable(name="user_role_connector",
joinColumns = {@JoinColumn(name="role_fk", referencedColumnName="role_pk")},
inverseJoinColumns = {@JoinColumn(name="user_fk", referencedColumnName="user_pk")}
)
private Set<UserAccount> userRoles;
为什么不映射?出现的错误是 bean 从按字母顺序排列的第一个控制器创建的错误,因此它一定是错误的映射。
那里有什么好的 Spring Security 教程吗?
编辑:忘记写这些类工作,直到添加到 userAccount 的“role”字段和添加到角色的 userRoles。并且这里只显示有错误的代码。抱歉英语不好。