Interface Restriction<X>
- Type Parameters:
X- The entity result type of the query
A rule for restricting query results. This allows restrictions to be added to
a
QuerySpecification by calling
restrict().
SelectionSpecification.create(Book.class)
.restrict(Restriction.like(Book_.title, "%Hibernate%", false))
.restrict(Restriction.greaterThan(Book_.pages, 100))
.sort(Order.desc(Book_.title))
.createQuery(session)
.getResultList();
Each restriction pairs an attribute of the
entity with a Range of allowed values for the attribute.
A parameter of a finder method or HQL query method may be declared with type Restriction<? super E>,
List<Restriction<? super E>>, or Restriction<? super E>...
(varargs) where E is the entity type returned by the query.
To create a Restriction on a compound path, use Path.
- Since:
- 7.0
- See Also:
- API Note:
- This class is similar to
jakarta.data.Restriction, and is used by Hibernate Data Repositories to implement Jakarta Data query methods.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Restriction<T> all(List<? extends Restriction<? super T>> restrictions) Combine the given restrictions using logical and.static <T> Restriction<T> all(Restriction<? super T>... restrictions) Combine the given restrictions using logical and.default Restriction<X> and(Restriction<X> restriction) Combine this restriction with the given restriction using logical and.static <T> Restriction<T> any(List<? extends Restriction<? super T>> restrictions) Combine the given restrictions using logical or.static <T> Restriction<T> any(Restriction<? super T>... restrictions) Combine the given restrictions using logical or.default voidapply(CriteriaQuery<?> query, Root<? extends X> root) Apply this restriction to the given root entity of the given criteria query.static <T, U extends Comparable<U>>
Restriction<T> between(SingularAttribute<T, U> attribute, U lowerBound, U upperBound) Restrict the given attribute to fall between the given values.static <T> Restriction<T> contains(SingularAttribute<T, String> attribute, String substring) Restrict the given attribute to contain the given substring.static <T> Restriction<T> contains(SingularAttribute<T, String> attribute, String substring, boolean caseSensitive) Restrict the given attribute to contain the given substring, explicitly specifying case sensitivity.static <T> Restriction<T> endsWith(SingularAttribute<T, String> attribute, String suffix) Restrict the given attribute to end with the given string suffix.static <T> Restriction<T> endsWith(SingularAttribute<T, String> attribute, String suffix, boolean caseSensitive) Restrict the given attribute to end with the given string suffix, explicitly specifying case sensitivity.static <T,U> Restriction <T> equal(SingularAttribute<T, U> attribute, U value) Restrict the given attribute to be exactly equal to the given value.static <T> Restriction<T> equalIgnoringCase(SingularAttribute<T, String> attribute, String value) Restrict the given attribute to be equal to the given string, ignoring case.static <T, U extends Comparable<U>>
Restriction<T> greaterThan(SingularAttribute<T, U> attribute, U lowerBound) Restrict the given attribute to be strictly greater than the given lower bound.static <T, U extends Comparable<U>>
Restriction<T> greaterThanOrEqual(SingularAttribute<T, U> attribute, U lowerBound) Restrict the given attribute to be greater than or equal to the given lower bound.static <T,U> Restriction <T> in(SingularAttribute<T, U> attribute, List<U> values) Restrict the given attribute to be exactly equal to one of the given values.static <T,U> Restriction <T> in(SingularAttribute<T, U> attribute, U... values) Restrict the given attribute to be exactly equal to one of the given values.static <T, U extends Comparable<U>>
Restriction<T> lessThan(SingularAttribute<T, U> attribute, U upperBound) Restrict the given attribute to be strictly less than the given upper bound.static <T, U extends Comparable<U>>
Restriction<T> lessThanOrEqual(SingularAttribute<T, U> attribute, U upperBound) Restrict the given attribute to be less than or equal to the given upper bound.static <T> Restriction<T> like(SingularAttribute<T, String> attribute, String pattern) Restrict the given attribute to match the given pattern.static <T> Restriction<T> like(SingularAttribute<T, String> attribute, String pattern, boolean caseSensitive) Restrict the given attribute to match the given pattern, explicitly specifying case sensitivity.static <T> Restriction<T> like(SingularAttribute<T, String> attribute, String pattern, boolean caseSensitive, char charWildcard, char stringWildcard) Restrict the given attribute to match the given pattern, explicitly specifying case sensitivity, along with single-character and multi-character wildcards.negated()Negate this restriction.static <T, U extends Comparable<U>>
Restriction<T> notBetween(SingularAttribute<T, U> attribute, U lowerBound, U upperBound) Restrict the given attribute to not fall between the given values.static <T> Restriction<T> notContains(SingularAttribute<T, String> attribute, String substring) Restrict the given attribute to not contain the given substring.static <T> Restriction<T> notContains(SingularAttribute<T, String> attribute, String substring, boolean caseSensitive) Restrict the given attribute to not contain the given substring, explicitly specifying case sensitivity.static <T,U> Restriction <T> notIn(SingularAttribute<T, U> attribute, List<U> values) Restrict the given attribute to be not equal to any of the given values.static <T,U> Restriction <T> notIn(SingularAttribute<T, U> attribute, U... values) Restrict the given attribute to be not equal to any of the given values.static <T> Restriction<T> notLike(SingularAttribute<T, String> attribute, String pattern) Restrict the given attribute to not match the given pattern.static <T> Restriction<T> notLike(SingularAttribute<T, String> attribute, String pattern, boolean caseSensitive) Restrict the given attribute to not match the given pattern, explicitly specifying case sensitivity.static <T,U> Restriction <T> notNull(SingularAttribute<T, U> attribute) Restrict the given attribute to be non-null.default Restriction<X> or(Restriction<X> restriction) Combine this restriction with the given restriction using logical or.static <T,U> Restriction <T> restrict(SingularAttribute<T, U> attribute, Range<U> range) Restrict the allowed values of the given attribute to the given range.static <T> Restriction<T> Restrict the allowed values of the named attribute of the given entity class to the given range.static <T> Restriction<T> startsWith(SingularAttribute<T, String> attribute, String prefix) Restrict the given attribute to start with the given string prefix.static <T> Restriction<T> startsWith(SingularAttribute<T, String> attribute, String prefix, boolean caseSensitive) Restrict the given attribute to start with the given string prefix, explicitly specifying case sensitivity.toPredicate(Root<? extends X> root, CriteriaBuilder builder) Return a JPA CriteriaPredicateconstraining the given root entity by this restriction.static <T,U> Restriction <T> unequal(SingularAttribute<T, U> attribute, U value) Restrict the given attribute to be not equal to the given value.static <T> Restriction<T> An empty restriction.
-
Method Details
-
negated
Negate this restriction. -
or
Combine this restriction with the given restriction using logical or.- See Also:
-
and
Combine this restriction with the given restriction using logical and.- See Also:
-
toPredicate
-
apply
Apply this restriction to the given root entity of the given criteria query. -
restrict
@Nonnull static <T,U> Restriction<T> restrict(@Nonnull SingularAttribute<T, U> attribute, @Nonnull Range<U> range) Restrict the allowed values of the given attribute to the given range. -
restrict
@Nonnull static <T> Restriction<T> restrict(@Nonnull Class<T> type, @Nonnull String attributeName, @Nonnull Range<?> range) Restrict the allowed values of the named attribute of the given entity class to the given range.This operation is not compile-time type safe. Prefer the use of
restrict(SingularAttribute, Range). -
equal
@Nonnull static <T,U> Restriction<T> equal(@Nonnull SingularAttribute<T, U> attribute, @Nonnull U value) Restrict the given attribute to be exactly equal to the given value.- See Also:
-
unequal
@Nonnull static <T,U> Restriction<T> unequal(@Nonnull SingularAttribute<T, U> attribute, @Nonnull U value) Restrict the given attribute to be not equal to the given value. -
equalIgnoringCase
@Nonnull static <T> Restriction<T> equalIgnoringCase(@Nonnull SingularAttribute<T, String> attribute, @Nonnull String value) Restrict the given attribute to be equal to the given string, ignoring case.- See Also:
-
in
@SafeVarargs @Nonnull static <T,U> Restriction<T> in(@Nonnull SingularAttribute<T, U> attribute, @Nonnull U... values) Restrict the given attribute to be exactly equal to one of the given values.- See Also:
-
notIn
@SafeVarargs @Nonnull static <T,U> Restriction<T> notIn(@Nonnull SingularAttribute<T, U> attribute, @Nonnull U... values) Restrict the given attribute to be not equal to any of the given values. -
in
@Nonnull static <T,U> Restriction<T> in(@Nonnull SingularAttribute<T, U> attribute, @Nonnull List<U> values) Restrict the given attribute to be exactly equal to one of the given values.- See Also:
-
notIn
@Nonnull static <T,U> Restriction<T> notIn(@Nonnull SingularAttribute<T, U> attribute, @Nonnull List<U> values) Restrict the given attribute to be not equal to any of the given values. -
between
@Nonnull static <T, U extends Comparable<U>> Restriction<T> between(@Nonnull SingularAttribute<T, U> attribute, @Nonnull U lowerBound, @Nonnull U upperBound) Restrict the given attribute to fall between the given values.- See Also:
-
notBetween
@Nonnull static <T, U extends Comparable<U>> Restriction<T> notBetween(@Nonnull SingularAttribute<T, U> attribute, @Nonnull U lowerBound, @Nonnull U upperBound) Restrict the given attribute to not fall between the given values. -
greaterThan
@Nonnull static <T, U extends Comparable<U>> Restriction<T> greaterThan(@Nonnull SingularAttribute<T, U> attribute, @Nonnull U lowerBound) Restrict the given attribute to be strictly greater than the given lower bound.- See Also:
-
lessThan
@Nonnull static <T, U extends Comparable<U>> Restriction<T> lessThan(@Nonnull SingularAttribute<T, U> attribute, @Nonnull U upperBound) Restrict the given attribute to be strictly less than the given upper bound.- See Also:
-
greaterThanOrEqual
@Nonnull static <T, U extends Comparable<U>> Restriction<T> greaterThanOrEqual(@Nonnull SingularAttribute<T, U> attribute, @Nonnull U lowerBound) Restrict the given attribute to be greater than or equal to the given lower bound.- See Also:
-
lessThanOrEqual
@Nonnull static <T, U extends Comparable<U>> Restriction<T> lessThanOrEqual(@Nonnull SingularAttribute<T, U> attribute, @Nonnull U upperBound) Restrict the given attribute to be less than or equal to the given upper bound.- See Also:
-
like
@Nonnull static <T> Restriction<T> like(@Nonnull SingularAttribute<T, String> attribute, @Nonnull String pattern, boolean caseSensitive, char charWildcard, char stringWildcard) Restrict the given attribute to match the given pattern, explicitly specifying case sensitivity, along with single-character and multi-character wildcards.- Parameters:
pattern- A pattern involving the given wildcard characterscaseSensitive-trueif matching is case-sensitivecharWildcard- A wildcard character which matches any single characterstringWildcard- A wildcard character which matches any string of characters- See Also:
-
like
@Nonnull static <T> Restriction<T> like(@Nonnull SingularAttribute<T, String> attribute, @Nonnull String pattern, boolean caseSensitive) Restrict the given attribute to match the given pattern, explicitly specifying case sensitivity. The pattern must be expressed in terms of the default wildcard characters_and%.- Parameters:
pattern- A pattern involving the default wildcard characterscaseSensitive-trueif matching is case-sensitive- See Also:
-
like
@Nonnull static <T> Restriction<T> like(@Nonnull SingularAttribute<T, String> attribute, @Nonnull String pattern) Restrict the given attribute to match the given pattern. The pattern must be expressed in terms of the default wildcard characters_and%.- See Also:
-
notLike
@Nonnull static <T> Restriction<T> notLike(@Nonnull SingularAttribute<T, String> attribute, @Nonnull String pattern) Restrict the given attribute to not match the given pattern. The pattern must be expressed in terms of the default wildcard characters_and%.- See Also:
-
notLike
@Nonnull static <T> Restriction<T> notLike(@Nonnull SingularAttribute<T, String> attribute, @Nonnull String pattern, boolean caseSensitive) Restrict the given attribute to not match the given pattern, explicitly specifying case sensitivity. The pattern must be expressed in terms of the default wildcard characters_and%.- Parameters:
pattern- A pattern involving the default wildcard characterscaseSensitive-trueif matching is case-sensitive
-
startsWith
@Nonnull static <T> Restriction<T> startsWith(@Nonnull SingularAttribute<T, String> attribute, @Nonnull String prefix) Restrict the given attribute to start with the given string prefix.- See Also:
-
endsWith
@Nonnull static <T> Restriction<T> endsWith(@Nonnull SingularAttribute<T, String> attribute, @Nonnull String suffix) Restrict the given attribute to end with the given string suffix.- See Also:
-
contains
@Nonnull static <T> Restriction<T> contains(@Nonnull SingularAttribute<T, String> attribute, @Nonnull String substring) Restrict the given attribute to contain the given substring.- See Also:
-
notContains
@Nonnull static <T> Restriction<T> notContains(@Nonnull SingularAttribute<T, String> attribute, @Nonnull String substring) Restrict the given attribute to not contain the given substring. -
startsWith
@Nonnull static <T> Restriction<T> startsWith(@Nonnull SingularAttribute<T, String> attribute, @Nonnull String prefix, boolean caseSensitive) Restrict the given attribute to start with the given string prefix, explicitly specifying case sensitivity.- See Also:
-
endsWith
@Nonnull static <T> Restriction<T> endsWith(@Nonnull SingularAttribute<T, String> attribute, @Nonnull String suffix, boolean caseSensitive) Restrict the given attribute to end with the given string suffix, explicitly specifying case sensitivity.- See Also:
-
contains
@Nonnull static <T> Restriction<T> contains(@Nonnull SingularAttribute<T, String> attribute, @Nonnull String substring, boolean caseSensitive) Restrict the given attribute to contain the given substring, explicitly specifying case sensitivity.- See Also:
-
notContains
@Nonnull static <T> Restriction<T> notContains(@Nonnull SingularAttribute<T, String> attribute, @Nonnull String substring, boolean caseSensitive) Restrict the given attribute to not contain the given substring, explicitly specifying case sensitivity. -
notNull
Restrict the given attribute to be non-null. -
all
@Nonnull static <T> Restriction<T> all(@Nonnull List<? extends Restriction<? super T>> restrictions) Combine the given restrictions using logical and. -
any
@Nonnull static <T> Restriction<T> any(@Nonnull List<? extends Restriction<? super T>> restrictions) Combine the given restrictions using logical or. -
all
@SafeVarargs @Nonnull static <T> Restriction<T> all(@Nonnull Restriction<? super T>... restrictions) Combine the given restrictions using logical and. -
any
@SafeVarargs @Nonnull static <T> Restriction<T> any(@Nonnull Restriction<? super T>... restrictions) Combine the given restrictions using logical or. -
unrestricted
An empty restriction.
-