使用inversejoincolumns时无法解析列

vmdwslir  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(309)

我正试图使用java在表之间建立多对多关系,但遇到了一个问题。到目前为止,我设计它的方式是用户<->角色<->权限,除了role.java中的最后一个“referencedcolumnname=”privilege\u id“没有被识别,我的ide也没有对此字段给出任何建议之外,所有的表和列都被识别。我不确定这是否是我的项目的主要问题,但当用户尝试登录时,我确实遇到throwlazyinitializationexception。
用户.java

package com.lukas.ramonas.cms.Model;

import javax.persistence.*;
import java.util.Collection;
import java.util.List;

/*******************************************
 * Defined user model

*******************************************/

@Entity
@Table(name = "user_table", schema = "public")
public class User {

    @Id
    @Column(name = "user_id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int user_id;

    private String name;

    private String username;

    private String password;

    private String email;

    private boolean confirmed;

    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinTable(
            name = "user_role_table",
            joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "role_id"))
    private Collection<Role> roles;

/*******************************************

* Setters and getters
*******************************************/

    public Integer getId() {
        return user_id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUsername() {
        return username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword() {
        return password;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEmail() {
        return email;
    }

    public void setConfirmed(Boolean confirmed) {
        this.confirmed = confirmed;
    }

    public Boolean getConfirmed() {
        return confirmed;
    }

    public void setRoles(Collection roles) {
        this.roles = roles;
    }

    public Collection<Role> getRoles() { return this.roles;
    }
}

角色.java

package com.lukas.ramonas.cms.Model;

import javax.persistence.*;
import java.util.Collection;

/*******************************************
 * Defined role model

*******************************************/

@Entity
@Table(name = "role_table", schema = "public")
public class Role {

    @Id
    @Column(name = "role_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int role_id;

    private String name;

    @ManyToMany(mappedBy = "roles")
    private Collection<User> users;

    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinTable(
            name = "role_privilege_table",
            joinColumns = @JoinColumn(name = "role_id", referencedColumnName = "role_id"),
            inverseJoinColumns = @JoinColumn(name = "privilege_id", referencedColumnName = "privilege_id"))
    private Collection<Privilege> privileges;

    public String getName() {
        return this.name;
    }

    public Collection<Privilege> getPrivileges() {
        return this.privileges;
    }
}

特权.java

package com.lukas.ramonas.cms.Model;

import javax.persistence.*;
import java.util.Collection;

/*******************************************
 * Defined privilege model

*******************************************/

@Entity
public class Privilege {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int privilege_id;

    private String name;

    @ManyToMany(mappedBy = "privileges")
    private Collection<Role> roles;

    public String getName() {
        return this.name;
    }
}

任何建议都非常感谢,如果你需要任何额外的信息,我很乐意提供。

hmmo2u0o

hmmo2u0o1#

所以解决这个问题非常简单。在privilege.java类中,我忘记添加@table注解。我可以通过将@table(name=“privilege\u table”,schema=“public”)添加到我的类中来解决这个问题:

package com.lukas.ramonas.cms.Model;

import javax.persistence.*;
import java.util.Collection;

/*******************************************
 * Defined privilege model

*******************************************/

@Entity
@Table(name = "privilege_table", schema = "public")
public class Privilege {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int privilege_id;

    private String name;

    @ManyToMany(mappedBy = "privileges")
    private Collection<Role> roles;

    public String getName() {
        return this.name;
    }
}

相关问题