Interface JpaSpecificationExecutor<T>
- All Known Subinterfaces:
JpaRepositoryImplementation<T,ID>
- All Known Implementing Classes:
SimpleJpaRepository
Specifications based on the JPA criteria API.- Author:
- Oliver Gierke, Christoph Strobl, Diego Krupitza, Mark Paluch, Joshua Chen
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceExtension toFluentQuery.FetchableFluentQueryallowing slice results and pagination with a custom countSpecification. -
Method Summary
Modifier and TypeMethodDescriptiondefault longcount(PredicateSpecification<T> spec) Returns the number of instances that the givenPredicateSpecificationwill return.longcount(Specification<T> spec) Returns the number of instances that the givenSpecificationwill return.longdelete(DeleteSpecification<T> spec) Deletes by theUpdateSpecificationand returns the number of rows deleted.default longdelete(PredicateSpecification<T> spec) Deletes by thePredicateSpecificationand returns the number of rows deleted.default booleanexists(PredicateSpecification<T> spec) Checks whether the data store contains elements that match the givenPredicateSpecification.booleanexists(Specification<T> spec) Checks whether the data store contains elements that match the givenSpecification.findAll(@Nullable Specification<T> spec, @Nullable Specification<T> countSpec, Pageable pageable) Returns aPageof entities matching the givenSpecification.findAll(PredicateSpecification<T> spec) Returns all entities matching the givenPredicateSpecification.findAll(Specification<T> spec) Returns all entities matching the givenSpecification.findAll(Specification<T> spec, Pageable pageable) Returns aPageof entities matching the givenSpecification.findAll(Specification<T> spec, Sort sort) Returns all entities matching the givenSpecificationandSort.default <S extends T, R>
RfindBy(PredicateSpecification<T> spec, Function<? super JpaSpecificationExecutor.SpecificationFluentQuery<S>, R> queryFunction) Returns entities matching the givenSpecificationapplying thequeryFunctionthat defines the query and its result type.findBy(Specification<T> spec, Function<? super JpaSpecificationExecutor.SpecificationFluentQuery<S>, R> queryFunction) Returns entities matching the givenSpecificationapplying thequeryFunctionthat defines the query and its result type.findOne(PredicateSpecification<T> spec) Returns a single entity matching the givenPredicateSpecificationorOptional.empty()if none found.findOne(Specification<T> spec) Returns a single entity matching the givenSpecificationorOptional.empty()if none found.longupdate(UpdateSpecification<T> spec) Updates entities by theUpdateSpecificationand returns the number of rows updated.
-
Method Details
-
findOne
Returns a single entity matching the givenPredicateSpecificationorOptional.empty()if none found.- Parameters:
spec- must not be null.- Returns:
- never null.
- Throws:
IncorrectResultSizeDataAccessException- if more than one entity found.- See Also:
-
findOne
Returns a single entity matching the givenSpecificationorOptional.empty()if none found.- Parameters:
spec- must not be null.- Returns:
- never null.
- Throws:
IncorrectResultSizeDataAccessException- if more than one entity found.- See Also:
-
findAll
Returns all entities matching the givenPredicateSpecification.- Parameters:
spec- must not be null.- Returns:
- never null.
- See Also:
-
findAll
Returns all entities matching the givenSpecification.- Parameters:
spec- must not be null.- Returns:
- never null.
- See Also:
-
findAll
Returns aPageof entities matching the givenSpecification.- Parameters:
spec- must not be null.pageable- must not be null.- Returns:
- never null.
- See Also:
-
findAll
Page<T> findAll(@Nullable Specification<T> spec, @Nullable Specification<T> countSpec, Pageable pageable) Returns aPageof entities matching the givenSpecification.Supports counting the total number of entities matching the
Specification.- Parameters:
spec- can be null, if noSpecificationis given all entities matching<T>will be selected.countSpec- can be null,if noSpecificationis given all entities matching<T>will be counted.pageable- must not be null.- Returns:
- never null.
- Since:
- 3.5
-
findAll
Returns all entities matching the givenSpecificationandSort.- Parameters:
spec- must not be null.sort- must not be null.- Returns:
- never null.
- See Also:
-
count
Returns the number of instances that the givenPredicateSpecificationwill return.- Parameters:
spec- thePredicateSpecificationto count instances for, must not be null.- Returns:
- the number of instances.
- See Also:
-
count
Returns the number of instances that the givenSpecificationwill return.- Parameters:
spec- theSpecificationto count instances for, must not be null.- Returns:
- the number of instances.
- See Also:
-
exists
Checks whether the data store contains elements that match the givenPredicateSpecification.- Parameters:
spec- thePredicateSpecificationto use for the existence check, must not be null.- Returns:
trueif the data store contains elements that match the givenPredicateSpecificationotherwisefalse.- See Also:
-
exists
Checks whether the data store contains elements that match the givenSpecification.- Parameters:
spec- theSpecificationto use for the existence check, must not be null.- Returns:
trueif the data store contains elements that match the givenSpecificationotherwisefalse.- See Also:
-
update
Updates entities by theUpdateSpecificationand returns the number of rows updated.This method uses
Criteria API bulk updatethat maps directly to database update operations. The persistence context is not synchronized with the result of the bulk update.- Parameters:
spec- theUpdateSpecificationto use for the update query must not be null.- Returns:
- the number of entities deleted.
- Since:
- 4.0
-
delete
Deletes by thePredicateSpecificationand returns the number of rows deleted.This method uses
Criteria API bulk deletethat maps directly to database delete operations. The persistence context is not synchronized with the result of the bulk delete.- Parameters:
spec- thePredicateSpecificationto use for the delete query, must not be null.- Returns:
- the number of entities deleted.
- Since:
- 3.0
- See Also:
-
delete
Deletes by theUpdateSpecificationand returns the number of rows deleted.This method uses
Criteria API bulk deletethat maps directly to database delete operations. The persistence context is not synchronized with the result of the bulk delete.- Parameters:
spec- theUpdateSpecificationto use for the delete query must not be null.- Returns:
- the number of entities deleted.
- Since:
- 3.0
- See Also:
-
findBy
default <S extends T, R> R findBy(PredicateSpecification<T> spec, Function<? super JpaSpecificationExecutor.SpecificationFluentQuery<S>, R> queryFunction) Returns entities matching the givenSpecificationapplying thequeryFunctionthat defines the query and its result type.The query object used with
queryFunctionis only valid inside thefindBy(…)method call. This requires the query function to return a query result and not theFluentQueryobject itself to ensure the query is executed inside thefindBy(…)method.- Parameters:
spec- must not be null.queryFunction- the query function defining projection, sorting, and the result type- Returns:
- all entities matching the given Example.
- Since:
- 4.0
-
findBy
<S extends T, R extends @Nullable Object> R findBy(Specification<T> spec, Function<? super JpaSpecificationExecutor.SpecificationFluentQuery<S>, R> queryFunction) Returns entities matching the givenSpecificationapplying thequeryFunctionthat defines the query and its result type.The query object used with
queryFunctionis only valid inside thefindBy(…)method call. This requires the query function to return a query result and not theFluentQueryobject itself to ensure the query is executed inside thefindBy(…)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 theFluentQueryinstance.- Since:
- 3.0
-