The JpaSpecificationExecutor provides a fluent API via the findBy method to execute queries derived from a Specification or PredicateSpecification. This allows dynamic control over query execution aspects.
Intermediate methods (to be used within the query function):
sortBy(Sort sort): Apply ordering. Repeated calls append sorts.limit(long limit): Limit result count.as(Class<R> type): Specify the type for projection.project(...): Limit query properties.
Terminal methods (to be used within the query function):
first(): Returns Optional<T> for the first result.firstValue(): Returns a nullable result for the first value.one(): Returns Optional<T> for exactly one result; throws IncorrectResultSizeDataAccessException if more than one is found.oneValue(): Returns a nullable result for exactly one value.all(): Returns all results as a List<T>.page(Pageable): Returns a Page<T>.slice(Pageable): Returns a Slice<T>.scroll(ScrollPosition): Returns a Window<T> using scrolling.stream(): Returns a stateful Stream<T> (must be closed).count(): Returns the count of matching entities.exists(): Returns whether any match exists.
Example: Get a projected Page ordered by lastname:
Page<CustomerProjection> page = repository.findBy(spec,
q -> q.as(CustomerProjection.class)
.page(PageRequest.of(0, 20, Sort.by("lastname")))
);