JPA Criteria API - List Matching in Spring JPA Data Specifications

I want to create a specification that matches the group identifier of the user object in relation to the list of identifiers. I was thinking about using isMember (as in the code), but the method will not accept the list.

public static Specification<User> matchCompanyIdsList(final List<Long> groupIds){ return new Specification<User>() { public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder builder){ final Path<Group> group = root.<Group> get("group"); return builder.isMember(company.<Long>get("id"), companyIds); } }; } 

If I am turned off, how would I do it otherwise?

+16
source share
2 answers

Do you want to create a specification that matches all users with a group identifier that are in the groupsIds list?

If so, you can use something like this (which will use the SQL IN clause):

 public static Specification<User> matchCompanyIdsList(final List<Long> groupIds){ return new Specification<User>() { public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder builder){ final Path<Group> group = root.<Group> get("group"); return group.in(groupIds); } }; } 
+27
source

path lamb

  static Specification<User> hasRoles(List<String> roles) { return (root, query, cb) -> { query.distinct(true); Join<User, Account> joinUserAccount = root.join(User_.account); Join<Account, AccountRole> acctRolesJoin = joinUserAccount.join(Account_.accountRoles); Join<AccountRole, Role> rolesJoin = acctRolesJoin.join(AccountRole_.role); return rolesJoin.get(Role_.name).in(roles); }; } 
0
source

All Articles