UserGroupSpecs { public static Specification<UserGroup> hasProfile(final Profile profile) { return new Specification<UserGroup>() { @Override public Predicate toPredicate(Root<UserGroup> root, CriteriaQuery<?> query, CriteriaBuilder cb) { Path<Profile> profileIdAttributePath = root.get(UserGroup_.id).get(UserGroupId_.profile); Predicate profileIdEqualPredicate = cb.equal(profileIdAttributePath, cb.literal(profile)); return profileIdEqualPredicate; } }; } private  UserGroupSpecs(); static Specification<UserGroup> hasGroupId(final int groupId); static Specification<UserGroup> hasGroupIds(final List<Integer> groupId); static Specification<UserGroup> hasUserId(final int userId); static Specification<UserGroup> hasProfile(final Profile profile); static Specification<UserGroup> isReservedGroup(final boolean isReservedGroup);  }
@Test public void testHasProfile() throws Exception { UserGroup ug1 = _repo.save(newUserGroup()); _repo.save(newUserGroup()); _repo.save(newUserGroup()); _repo.save(newUserGroup()); List<UserGroup> found = _repo.findAll(hasProfile(ug1.getProfile())); for (UserGroup userGroup : found) { assertEquals(ug1.getProfile(), userGroup.getProfile()); } }