如何将我的用户表与角色表进行自动连接

huangapple go评论65阅读模式
英文:

How do I autojoin my users table with roles table

问题

我的用户表如下:

emp_user 表
--------------
id(bigint) 主键
uuid(character varying) 唯一
username(character varying) 唯一
-- 表中未定义外键

UserEntity 类如下:

@Entity
@Table(name = "emp_user")
public class UserEntity {
    @Id
    private String uuid; // 由于 Keycloak 内部原因
    
    @Column(name = "\"user\"")
    private String username;
    
    @Column(name = "id")
    private Long identifier;
    
    @OneToMany
    JoinColumn(referencedColumnName = "emp_user_id")
    private List<Roles> rolesList;
    
    // 获取器和设置器
}

一个用户可以有多个角色。

emp_user_roles 表
--------------------
emp_user_id(bigint)
emp_user_role(character varying)
-- emp_user_id 和 emp_user_role 共同形成键
-- 表中未定义外键

Roles 类如下:

@Entity
@Table(name = "emp_user_roles")
public class Roles implements Serializable {
    @Id
    @Column(name = "emp_user_id")
    private Long userId;
    
    @Id
    @Column(name = "emp_user_role")
    private String userRole;
    
    // 获取器和设置器
}

我得到了一个结果,一个用户,带有 rolesList,但其中没有角色,而只有一个错误消息,其中写着 无法评估表达式,方法引发了 'org.hibernate.exception.GenericJDBCException' 异常。

我认为我做错了什么。

英文:

My users table is as following:

emp_user table
--------------
id (bigint) PK
uuid (character varying) unique
username (character varying) unique
-- no foreign keys are defined in the table

The UserEntity class is as following:

@Entity
@Table(name = &quot;emp_user&quot;)
public class UserEntity {
    @Id
    private String uuid; // Because of Keycloak internals
    
    @Column(name = &quot;\&quot;user\&quot;&quot;)
    private String username;
    
    @Column(name = &quot;id&quot;)
    private Long identifier;
    
    @OneToMany
    JoinColumn(referencedColumnName = &quot;emp_user_id&quot;)
    private List&lt;Roles&gt; rolesList;
    
    // getters and setters
}

A user can has many roles.

emp_user_roles table
--------------------
emp_user_id (bigint)
emp_user_role (character varying)
-- both emp_user_id and emp_user_role together is a key
-- no foreign keys are defined in the table

The Roles class is as following:

@Entity
@Table(name = &quot;emp_user_roles&quot;)
public class Roles implements Serializable {
    @Id
    @Column(name = &quot;emp_user_id&quot;)
    private Long userId;
    
    @Id
    @Column(name = &quot;emp_user_role&quot;)
    private String userRole;
    
    // getters and setters
}

I get a result, one user, with rolesList but inside of it there are no roles but only an error message saying Unable to evaluate the expression Method threw &#39;org.hibernate.exception.GenericJDBCException&#39; exception.

I think I'm doing something wrong.

答案1

得分: 1

你必须使用“多对多”关联,因为一个“角色”可以有多个用户。在这种情况下,Hibernate会创建一个联接表。

@Entity
public class User {

@Id
@GeneratedValue
private Long id;

@ManyToMany(fetch = FetchType.LAZY)
private List<Role> roles;

}

@Entity
public class Role {

@Id
private Long id;

@Column
private String name;

}

你可以创建一个额外的实体“UserRoleMapping”,使用“@ManyToOne”关联到“User”和“@ManyToOne”关联到“Role”。这将简化添加或删除“Role”的过程。您将无需获取所有角色列表即可删除其中之一。另外,您可以向“UserRoleMapping”添加唯一约束(用户、角色)。

英文:

You have to use many-to-many association because a Role can have multiple users. Hibernate will create a join table in such case.

@Entity
public class User {

    @Id
    @GeneratedValue
    private Long id;

    @ManyToMany(fetch = FetchType.LAZY)
    private List&lt;Role&gt; roles;

}

@Entity
public class Role {

    @Id
    private Long id;

    @Column
    private String name;

}

You can create an additional entity UserRoleMapping with @ManyToOne to User and @ManyToOne to Role. It will be simply to add or delete Role. You will not need to get all roles list to remove one of them. Also you can add unique constraint (User, Role) to the UserRoleMapping.

答案2

得分: 0

以下是我解决这个问题的方法:

@Entity
@Table(name = "emp_user")
public class UserEntity implements Serializable { // 必须实现Serializable接口
    @Id
    private String uuid;

    @Column(name = "\"user\"")
    private String username;

    @Column(name = "id")
    private Long identifier;

    @OneToMany
    @JoinColumn(name = "emp_user_id", referencedColumnName = "id") // 名称和referencedColumnName都是必需的
    private List<Roles> rolesList;

    // 获取器和设置器
}
英文:

Here is how I have solved the issue:

@Entity
@Table(name = &quot;emp_user&quot;)
public class UserEntity implements Serializable { // &lt;-- must implement Serializable
    @Id
    private String uuid;

    @Column(name = &quot;\&quot;user\&quot;&quot;)
    private String username;

    @Column(name = &quot;id&quot;)
    private Long identifier;

    @OneToMany
    @JoinColumn(name = &quot;emp_user_id&quot;, referencedColumnName = &quot;id&quot;) // &lt;-- both name and referencedColumnName are required
    private List&lt;Roles&gt; rolesList;

    // getters and setters
}

huangapple
  • 本文由 发表于 2020年9月2日 16:27:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63701602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定