Home > database >  Spring boot write predicate to retrieve user from given role list
Spring boot write predicate to retrieve user from given role list

Time:02-10

I am trying to create a predicate to retrieve users having certain Role list (Role is a separate Table ) I have created predicate for other fields in User entity only unable to create the proper criteria for the above mentioned scenario. below are the 2 entity classes

User:

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

Role


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


}

I have to use predicates No other option. guide me how to write a predicate to get users who are having given Role list.

CodePudding user response:

If you refer to Java 8 Predicate class:

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

If you want a more complex predicate:

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

Take care while comparing List objects like that. Should be better to check if the user has certains roles (user.getRoles().contains(role))

If you refer to JPA Specification:

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());
   }
}
  • Related