Spring 引导写入谓词以从给定角色列表中检索用户

Spring boot write predicate to retrieve user from given role list

我正在尝试创建一个谓词来检索具有特定角色列表的用户(角色是一个单独的 Table ) 我为用户实体中的其他字段创建了谓词,但无法为上述场景创建适当的条件。 下面是 2 个实体 类

用户:

@Entity
@Table(name = "user")
@Data
public class User implements Serializable {
    public User() {
    }

    public User(User user) {
        this.username = user.getUsername();
        this.password = user.getPassword();
        this.email = user.getEmail();
        this.fullName = user.getFullName();
        this.address = user.getAddress();
        this.contactNo = user.getContactNo();
        this.enabled = user.isEnabled();
        this.accountNonExpired = user.isAccountNonExpired();
        this.credentialsNonExpired = user.isCredentialsNonExpired();
        this.accountNonLocked = user.isAccountNonLocked();
        this.roles = user.getRoles();
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "username")
    private String username;
    @Column(name = "fullName")
    private String fullName;
    @Column(name = "contactNo")
    private String contactNo;
    @Column(name = "address")
    private String address;
    @Column(name = "password")
    private String password;
    @Column(name = "email")
    private String email;
    @Column(name = "enabled")
    private boolean enabled;
    @Column(name = "accountNonExpired")
    private boolean accountNonExpired;
    @Column(name = "credentialsNonExpired")
    private boolean credentialsNonExpired;
    @Column(name = "accountNonLocked")
    private boolean accountNonLocked;


    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "role_user", joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
            inverseJoinColumns = {
                    @JoinColumn(name = "role_id", referencedColumnName = "id")})
    private List<Role> roles;
}

角色


@Entity
@Table(name = "role")
@Data
public class Role implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    @Column(name = "name")
    private String name;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "permission_role", joinColumns = {
            @JoinColumn(name = "role_id", referencedColumnName = "id")}, inverseJoinColumns = {
            @JoinColumn(name = "permission_id", referencedColumnName = "id")})
    private List<Permission> permissions;


}

我必须使用谓词没有其他选择。 指导我如何编写谓词来获取给定角色列表的用户。

如果参考Java 8谓词class:

Predicate<User> userPredicate = user -> user.getRoles().equals(roleListArg);

如果你想要更复杂的谓词:

Predicate<User> userPredicate = user -> { 
     ... your code here
};

像这样比较 List 对象时要小心。应该更好地检查用户是否具有某些角色 (user.getRoles().contains(role))

如果您参考 JPA 规范:

public Specification<User> getUserByRoles(List<Role> roles) {
   return (root, query, criteriaBuilder) => {
      Join<Role, User> join = root.join("role");
      //Return the condition you want to build, this is an example to get by role id
      return criteriaBuilder.in(join.get(id), roles.get(0).getId());
   }
}