简体   繁体   中英

Distinct results from Spring Data JPA Specification that uses join

I have the following Specification that I use to query for any Contact entities that are tied to certain ManagedApplication entities. I pass in a Collection<Long> that contains the ids of the ManagedApplication entities that I am searching for.

public static Specification<Contact> findByApp(final Collection<Long> appIds) {
    return new Specification<Contact>() {
        @Override
        public Predicate toPredicate(Root<Contact> root, CriteriaQuery<?> query, CriteriaBuilder cb) {            
            final Predicate appPredicate = root.join(Contact_.managedApplications)
                .get(ManagedApplication_.managedApplicationId).in(appIds);
        }
    }
}

I pass this specification to the .findAll() method of my PagingAndSoringRepository to retrieve a Page<Contact> that will contain all Contact entities that meet the search criteria.

Here is the Repository .

@Repository
public interface PagingAndSortingContactRepository extends PagingAndSortingRepository<Contact, Long>, JpaSpecificationExecutor<Contact> {    
}

And here is how I'm calling the .findAll() method.

final Page<Contact> contacts = pagingAndSortingContactRepository.findAll(ContactSpecification.findByApp(appIds), pageable);

This works and returns all Contact entities that are tied to any of the ManagedApplication entities that correspond to the ids passed in. However, since I am calling .join() to join the Contact entity with the ManagedApplication entity, if one Contact has multiple ManagedApplication entities in the list of app ids, then the query will return duplicate Contact entities.

So what I need to know is, how can I get only distinct Contact entities returned from my query using this Specification ?

I know that CriteriaQuery has a .distinct() method that you can pass a boolean value to, but I am not using the CriteriaQuery instance in the toPredicate() method of my Specification .

Here are the relevant sections of my metamodels.

Contact_.java:

@StaticMetamodel(Contact.class)
public class Contact_ {
    public static volatile SingularAttribute<Contact, String> firstNm;
    public static volatile SingularAttribute<Contact, String> lastNm;
    public static volatile SingularAttribute<Contact, String> emailAddress;
    public static volatile SetAttribute<Contact, ManagedApplication> managedApplications;
    public static volatile SetAttribute<Contact, ContactToStructure> contactToStructures;
}

ManagedApplication_.java

@StaticMetamodel(ManagedApplication.class)
public class ManagedApplication_ {
    public static volatile SingularAttribute<ManagedApplication, Integer> managedApplicationId;
}

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);
    ...

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(..))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM