UserSpecs { public static Specification<User> hasProfile(final Profile profile) { return new Specification<User>() { @Override public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) { Path<Profile> profilePath = root.get(User_.profile); Predicate userIdEqualPredicate = cb.equal(profilePath, cb.literal(profile)); return userIdEqualPredicate; } }; } private  UserSpecs(); static Specification<User> hasUserId(final int userId); static Specification<User> hasProfile(final Profile profile); static Specification<User> hasUserName(final String userName); static Specification<User> hasEmail(final String email); static Specification<User> hasNullAuthType(); static Specification<User> hasAuthType(final String authType); static Specification<User> userIsNameNotOneOf(final Collection<String> usernames); static Specification<User> hasUserIdIn(final Collection<Integer> ids); static Specification<User> hasEnabled(final Boolean enabled); static Specification<User> loginDateBetweenAndByGroups(final ISODate loginDateFrom,
                                                                  final ISODate loginDateTo,
                                                                  final Collection<Integer> groups);  }
@Test public void testHasProfile() throws Exception { final User user1 = newUser(_inc); user1.setProfile(Profile.RegisteredUser); _userRepo.save(user1); final User user2 = newUser(_inc); user2.setProfile(Profile.Reviewer); _userRepo.save(user2); List<User> found = _userRepo.findAll(hasProfile(Profile.RegisteredUser)); assertEquals(1, found.size()); assertEquals(user1.getId(), found.get(0).getId()); found = _userRepo.findAll(hasProfile(Profile.Reviewer)); assertEquals(1, found.size()); assertEquals(user2.getId(), found.get(0).getId()); }