Distinct results from Spring Data JPA Specification that uses join
a new static method could be added
public static Specification<Object> distinct() {
return (root, query, cb) -> {
query.distinct(true);
return null;
};
}
where later you could add when creating your Specification
Specification.where(
YourStaticClassWhereYouCreatedTheUpperMethod.distinct().and(..))
Use the query
parameter in your toPredicate
method to invoke the distinct method.
Sample below:
public Predicate toPredicate(Root<Contact> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
final Predicate appPredicate = root.join(Contact_.managedApplications)
.get(ManagedApplication_.managedApplicationId).in(appIds);
query.distinct(true);
...