Interface JpaSpecificationExecutor<T>

All Known Subinterfaces:
JpaRepositoryImplementation<T,ID>
All Known Implementing Classes:
QuerydslJpaRepository, SimpleJpaRepository

public interface JpaSpecificationExecutor<T>
Interface to allow execution of Specifications based on the JPA criteria API.
Author:
Oliver Gierke, Christoph Strobl, Diego Krupitza, Mark Paluch
  • Method Details

    • findOne

      Optional<T> findOne(Specification<T> spec)
      Returns a single entity matching the given Specification or Optional.empty() if none found.
      Parameters:
      spec - must not be null.
      Returns:
      never null.
      Throws:
      IncorrectResultSizeDataAccessException - if more than one entity found.
    • findAll

      List<T> findAll(@Nullable Specification<T> spec)
      Returns all entities matching the given Specification.

      If no Specification is given all entities matching <T> will be selected.

      Parameters:
      spec - can be null.
      Returns:
      never null.
    • findAll

      Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable)
      Returns a Page of entities matching the given Specification.

      If no Specification is given all entities matching <T> will be selected.

      Parameters:
      spec - can be null.
      pageable - must not be null.
      Returns:
      never null.
    • findAll

      List<T> findAll(@Nullable Specification<T> spec, Sort sort)
      Returns all entities matching the given Specification and Sort.

      If no Specification is given all entities matching <T> will be selected.

      Parameters:
      spec - can be null.
      sort - must not be null.
      Returns:
      never null.
    • count

      long count(@Nullable Specification<T> spec)
      Returns the number of instances that the given Specification will return.

      If no Specification is given all entities matching <T> will be counted.

      Parameters:
      spec - the Specification to count instances for, must not be null.
      Returns:
      the number of instances.
    • exists

      boolean exists(Specification<T> spec)
      Checks whether the data store contains elements that match the given Specification.
      Parameters:
      spec - the Specification to use for the existence check, ust not be null.
      Returns:
      true if the data store contains elements that match the given Specification otherwise false.
    • delete

      long delete(@Nullable Specification<T> spec)
      Deletes by the Specification and returns the number of rows deleted.

      This method uses Criteria API bulk delete that maps directly to database delete operations. The persistence context is not synchronized with the result of the bulk delete.

      Please note that CriteriaQuery in, Specification.toPredicate(Root, CriteriaQuery, CriteriaBuilder) will be null because CriteriaBuilder.createCriteriaDelete(Class) does not implement CriteriaQuery.

      If no Specification is given all entities matching <T> will be deleted.

      Parameters:
      spec - the Specification to use for the existence check, can not be null.
      Returns:
      the number of entities deleted.
      Since:
      3.0
    • findBy

      <S extends T, R> R findBy(Specification<T> spec, Function<FluentQuery.FetchableFluentQuery<S>,R> queryFunction)
      Returns entities matching the given Specification applying the queryFunction that defines the query and its result type.

      The query object used with queryFunction is only valid inside the findBy(…) method call. This requires the query function to return a query result and not the FluentQuery object itself to ensure the query is executed inside the findBy(…) method.

      Parameters:
      spec - must not be null.
      queryFunction - the query function defining projection, sorting, and the result type
      Returns:
      all entities matching the given specification.
      Throws:
      InvalidDataAccessApiUsageException - if the query function returns the FluentQuery instance.
      Since:
      3.0