Interface Specification<T>
- All Superinterfaces:
Serializable
- All Known Implementing Classes:
KeysetScrollSpecification
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Specification in the sense of Domain Driven Design.
Specifications can be composed into higher order functions from other specifications using
and(Specification)
, or(Specification)
or factory methods such as allOf(Iterable)
.
Composition considers whether one or more specifications contribute to the overall predicate by returning a
Predicate
or null. Specifications returning null are considered to not contribute to
the overall predicate and their result is not considered in the final predicate.
- Author:
- Oliver Gierke, Thomas Darimont, Krzysztof Rzymkowski, Sebastian Staudt, Mark Paluch, Jens Schauder, Daniel Shuy, Sergey Rukin
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Specification<T>
allOf
(Iterable<Specification<T>> specifications) Applies an AND operation to all the givenSpecification
s.static <T> Specification<T>
allOf
(Specification<T>... specifications) Applies an AND operation to all the givenSpecification
s.default Specification<T>
and
(PredicateSpecification<T> other) ANDs the givenSpecification
to the current one.default Specification<T>
and
(Specification<T> other) ANDs the givenSpecification
to the current one.static <T> Specification<T>
anyOf
(Iterable<Specification<T>> specifications) Applies an OR operation to all the givenSpecification
s.static <T> Specification<T>
anyOf
(Specification<T>... specifications) Applies an OR operation to all the givenSpecification
s.static <T> Specification<T>
not
(Specification<T> spec) Negates the givenSpecification
.default Specification<T>
or
(PredicateSpecification<T> other) ORs the given specification to the current one.default Specification<T>
or
(Specification<T> other) ORs the given specification to the current one.@Nullable jakarta.persistence.criteria.Predicate
toPredicate
(jakarta.persistence.criteria.Root<T> root, jakarta.persistence.criteria.CriteriaQuery<?> query, jakarta.persistence.criteria.CriteriaBuilder criteriaBuilder) Creates a WHERE clause for a query of the referenced entity in form of aPredicate
for the givenRoot
andCriteriaUpdate
.static <T> Specification<T>
Simple static factory method to create a specification matching all objects.static <T> Specification<T>
where
(PredicateSpecification<T> spec) Simple static factory method to add some syntactic sugar translatingPredicateSpecification
toSpecification
.
-
Method Details
-
unrestricted
Simple static factory method to create a specification matching all objects.- Type Parameters:
T
- the type of theRoot
the resulting Specification operates on.- Returns:
- guaranteed to be not null.
-
where
Simple static factory method to add some syntactic sugar translatingPredicateSpecification
toSpecification
.- Type Parameters:
T
- the type of theRoot
the resulting Specification operates on.- Parameters:
spec
- thePredicateSpecification
to wrap.- Returns:
- guaranteed to be not null.
-
and
ANDs the givenSpecification
to the current one.- Parameters:
other
- the otherSpecification
.- Returns:
- the conjunction of the specifications.
- Since:
- 2.0
-
and
@Contract("_ -> new") @CheckReturnValue default Specification<T> and(PredicateSpecification<T> other) ANDs the givenSpecification
to the current one.- Parameters:
other
- the otherPredicateSpecification
.- Returns:
- the conjunction of the specifications.
- Since:
- 2.0
-
or
ORs the given specification to the current one.- Parameters:
other
- the otherSpecification
.- Returns:
- the disjunction of the specifications
- Since:
- 2.0
-
or
@Contract("_ -> new") @CheckReturnValue default Specification<T> or(PredicateSpecification<T> other) ORs the given specification to the current one.- Parameters:
other
- the otherPredicateSpecification
.- Returns:
- the disjunction of the specifications
- Since:
- 2.0
-
not
Negates the givenSpecification
.- Type Parameters:
T
- the type of theRoot
the resulting Specification operates on.- Parameters:
spec
- can be null.- Returns:
- guaranteed to be not null.
- Since:
- 2.0
-
allOf
Applies an AND operation to all the givenSpecification
s. Ifspecifications
is empty, the resultingSpecification
will be unrestricted applying to all objects.- Parameters:
specifications
- theSpecification
s to compose.- Returns:
- the conjunction of the specifications.
- Since:
- 3.0
- See Also:
-
allOf
Applies an AND operation to all the givenSpecification
s. Ifspecifications
is empty, the resultingSpecification
will be unrestricted applying to all objects.- Parameters:
specifications
- theSpecification
s to compose.- Returns:
- the conjunction of the specifications.
- Since:
- 3.0
- See Also:
-
anyOf
Applies an OR operation to all the givenSpecification
s. Ifspecifications
is empty, the resultingSpecification
will be unrestricted applying to all objects.- Parameters:
specifications
- theSpecification
s to compose.- Returns:
- the disjunction of the specifications
- Since:
- 3.0
- See Also:
-
anyOf
Applies an OR operation to all the givenSpecification
s. Ifspecifications
is empty, the resultingSpecification
will be unrestricted applying to all objects.- Parameters:
specifications
- theSpecification
s to compose.- Returns:
- the disjunction of the specifications
- Since:
- 3.0
- See Also:
-
toPredicate
@Nullable jakarta.persistence.criteria.Predicate toPredicate(jakarta.persistence.criteria.Root<T> root, jakarta.persistence.criteria.CriteriaQuery<?> query, jakarta.persistence.criteria.CriteriaBuilder criteriaBuilder) Creates a WHERE clause for a query of the referenced entity in form of aPredicate
for the givenRoot
andCriteriaUpdate
.- Parameters:
root
- must not be null.query
- the criteria query.criteriaBuilder
- must not be null.- Returns:
- a
Predicate
, may be null.
-