Interface JpaSpecificationExecutor<T>
- All Known Subinterfaces:
JpaRepositoryImplementation<T,
ID>
- All Known Implementing Classes:
QuerydslJpaRepository
,SimpleJpaRepository
Specification
s based on the JPA criteria API.- Author:
- Oliver Gierke, Christoph Strobl, Diego Krupitza, Mark Paluch, Joshua Chen
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interface
Extension toFluentQuery.FetchableFluentQuery
allowing slice results and pagination with a custom countSpecification
. -
Method Summary
Modifier and TypeMethodDescriptionlong
count
(Specification<T> spec) Returns the number of instances that the givenSpecification
will return.long
delete
(Specification<T> spec) Deletes by theSpecification
and returns the number of rows deleted.boolean
exists
(Specification<T> spec) Checks whether the data store contains elements that match the givenSpecification
.findAll
(Specification<T> spec) Returns all entities matching the givenSpecification
.findAll
(Specification<T> spec, Pageable pageable) Returns aPage
of entities matching the givenSpecification
.findAll
(Specification<T> spec, Sort sort) Returns all entities matching the givenSpecification
andSort
.findAll
(Specification<T> spec, Specification<T> countSpec, Pageable pageable) Returns aPage
of entities matching the givenSpecification
.<S extends T,
R>
RfindBy
(Specification<T> spec, Function<? super JpaSpecificationExecutor.SpecificationFluentQuery<S>, R> queryFunction) Returns entities matching the givenSpecification
applying thequeryFunction
that defines the query and its result type.findOne
(Specification<T> spec) Returns a single entity matching the givenSpecification
orOptional.empty()
if none found.
-
Method Details
-
findOne
Returns a single entity matching the givenSpecification
orOptional.empty()
if none found.- Parameters:
spec
- must not be null.- Returns:
- never null.
- Throws:
IncorrectResultSizeDataAccessException
- if more than one entity found.
-
findAll
Returns all entities matching the givenSpecification
.If no
Specification
is given all entities matching<T>
will be selected.- Parameters:
spec
- can be null.- Returns:
- never null.
-
findAll
Returns aPage
of entities matching the givenSpecification
.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
Page<T> findAll(@Nullable Specification<T> spec, @Nullable Specification<T> countSpec, Pageable pageable) Returns aPage
of entities matching the givenSpecification
.Supports counting the total number of entities matching the
Specification
.- Parameters:
spec
- can be null, if noSpecification
is given all entities matching<T>
will be selected.countSpec
- can be null,if noSpecification
is 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 givenSpecification
andSort
.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
Returns the number of instances that the givenSpecification
will return.If no
Specification
is given all entities matching<T>
will be counted.- Parameters:
spec
- theSpecification
to count instances for, must not be null.- Returns:
- the number of instances.
-
exists
Checks whether the data store contains elements that match the givenSpecification
.- Parameters:
spec
- theSpecification
to use for the existence check, ust not be null.- Returns:
true
if the data store contains elements that match the givenSpecification
otherwisefalse
.
-
delete
Deletes by theSpecification
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 becauseCriteriaBuilder.createCriteriaDelete(Class)
does not implementCriteriaQuery
.If no
Specification
is given all entities matching<T>
will be deleted.- Parameters:
spec
- theSpecification
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<? super JpaSpecificationExecutor.SpecificationFluentQuery<S>, R> queryFunction) Returns entities matching the givenSpecification
applying thequeryFunction
that defines the query and its result type.The query object used with
queryFunction
is only valid inside thefindBy(…)
method call. This requires the query function to return a query result and not theFluentQuery
object 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 theFluentQuery
instance.- Since:
- 3.0
-