java - 如何通过 spring-data 为 @ManyToMany 加载获取选择?

标签 java spring postgresql spring-data many-to-many

我尝试通过 @ManyToMany 使用 User 实体加载用户角色以进行身份​​验证,但出现异常。

ERROR: column user0_.role_id does not exist

刚刚开始学习spring data。请帮我解决这个问题。谢谢。

用户实体:

@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)
);

最佳答案

错误表示您在表 user 中没有属性 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 就可以了:)

关于java - 如何通过 spring-data 为 @ManyToMany 加载获取选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45803347/

相关文章:

node.js - 防止 sequelize 在 node.js 应用程序中删除数据库

java - 如何仅使用给定的格式将给定的字符串转换为java中的日期?

Java Swing 不是线程安全的;这是否意味着没有两个人可以修改其特定功能,例如同时JcomboBox

java - 如何在 Spring MVC 中使用绝对路径加载图像?

java - 如何在 Spring Security 中允许多重登录?

django - 使用 Django 的 ORM 在每个消息线程中查找最新消息

java - 为什么我要使用 Android Studio 的 "Expand Boolean"Intention?

java - 具有多个查询的 jdbcTemplate 查询 - spring boot

java - 未终止 <表格 :input tag

postgresql - 是否可以在完全具体化 View 之前回答对 View 的查询?