JPA Query Expressions (JPQL / Criteria)
Query expressions are the foundations on which JPQL and criteria queries are built.
Every query consists of clauses - SELECT, FROM, WHERE, GROUP BY, HAVING and ORDER BY, and each clause consists of JPQL / Criteria query expressions.
Atomic Expressions
The atomic query expressions are:
Every query expression consists of at least one atomic component. More complex query expressions are built by combining atomic expressions with operators and functions.
Operators and Functions
JPQL / Criteria queries support the following operators (in order of decreasing precedence):
- Navigation operator (.)
- Arithmetic operators:
*
(multiplication),/
(division), + (addition) and-
(subtraction). - Comparison operators:
=, <>, <, <=,>, >=, IS [NOT] NULL, [NOT] BETWEEN,
including Collection operators: [NOT] IN, IS [NOT] EMPTY, [NOT] MEMBER [OF]
and the [NOT] LIKE operator. - Logical operators: AND, OR, NOT.
In addition, JPA queries support predefined functions, which are also described in this section.
Organization of this Section
This section contains the following pages:
- Literals in JPQL and Criteria Queries
- Paths and Types in JPQL and Criteria API
- Numbers in JPQL and Criteria Queries
- Strings in JPQL and Criteria Queries
- Date and Time in JPQL and Criteria Queries
- Collections in JPQL and Criteria Queries
- Comparison in JPQL and Criteria API
- Logical Operators in JPQL and Criteria API
Detailed explanations on how to build criteria query expressions are provided as follows:
- Literals and Dates (
literalliteral(value)CriteriaBuilder's methodCreate an expression for a literal.See JavaDoc Reference Page...
,nullLiteral
nullLiteral(resultClass)CriteriaBuilder's methodCreate an expression for a null literal with the given type.See JavaDoc Reference Page...,currentDate
currentDate()CriteriaBuilder's methodCreate expression to return current date.See JavaDoc Reference Page..., ...). - Paths, navigation and types (
getget(attributeName)Path's methodCreate a path corresponding to the referenced attribute.See JavaDoc Reference Page...
,type
type()Path's methodCreate an expression corresponding to the type of the path.See JavaDoc Reference Page...). - Arithmetic expressions (
sum
sum(x, y)CriteriaBuilder's methodCreate an expression that returns the sum of its arguments.See JavaDoc Reference Page...,diff
diff(x, y)CriteriaBuilder's methodCreate an expression that returns the difference between its arguments.See JavaDoc Reference Page...,prod
prod(x, y)CriteriaBuilder's methodCreate an expression that returns the product of its arguments.See JavaDoc Reference Page...,quot
quot(x, y)CriteriaBuilder's methodCreate an expression that returns the quotient of its arguments.See JavaDoc Reference Page...,mod
mod(x, y)CriteriaBuilder's methodCreate an expression that returns the modulus of its arguments.See JavaDoc Reference Page...,abs
abs(x)CriteriaBuilder's methodCreate an expression that returns the absolute value of its argument.See JavaDoc Reference Page...,neg
neg(x)CriteriaBuilder's methodCreate an expression that returns the arithmetic negation of its argument.See JavaDoc Reference Page...,sqrt
sqrt(x)CriteriaBuilder's methodCreate an expression that returns the square root of its argument.See JavaDoc Reference Page...). - String expressions (
like
like(x, pattern)CriteriaBuilder's methodCreate a predicate for testing whether the expression satisfies the given pattern.See JavaDoc Reference Page...,length
length(x)CriteriaBuilder's methodCreate expression to return length of a string.See JavaDoc Reference Page...,locate
locate(x, pattern)CriteriaBuilder's methodCreate expression to locate the position of one string within another, returning position of first character if found.See JavaDoc Reference Page...,lower
lower(x)CriteriaBuilder's methodCreate expression for converting a string to lowercase.See JavaDoc Reference Page...,upper
upper(x)CriteriaBuilder's methodCreate expression for converting a string to uppercase.See JavaDoc Reference Page...,concat
concat(x, y)CriteriaBuilder's methodCreate an expression for string concatenation.See JavaDoc Reference Page...,substringsubstring(x, from)CriteriaBuilder's methodCreate an expression for substring extraction.See JavaDoc Reference Page..., ...
). - Data expressions (
currentDate
currentDate()CriteriaBuilder's methodCreate expression to return current date.See JavaDoc Reference Page...,currentTime
currentTime()CriteriaBuilder's methodCreate expression to return current time.See JavaDoc Reference Page...,currentTimestamp
currentTimestamp()CriteriaBuilder's methodCreate expression to return current timestamp.See JavaDoc Reference Page...). - Collection expressions (
isEmpty
isEmpty(collection)CriteriaBuilder's methodCreate a predicate that tests whether a collection is empty.See JavaDoc Reference Page...,isNotEmpty
isNotEmpty(collection)CriteriaBuilder's methodCreate a predicate that tests whether a collection is not empty.See JavaDoc Reference Page...,isMember
isMember(elem, collection)CriteriaBuilder's methodCreate a predicate that tests whether an element is a member of a collection.See JavaDoc Reference Page...,isNotMember
isNotMember(elem, collection)CriteriaBuilder's methodCreate a predicate that tests whether an element is not a member of a collection.See JavaDoc Reference Page...,size
size(collection)CriteriaBuilder's methodCreate an expression that tests the size of a collection.See JavaDoc Reference Page...). - Comparison expressions (
equal
size(collection)CriteriaBuilder's methodCreate an expression that tests the size of a collection.See JavaDoc Reference Page...,notEqual
notEqual(x, y)CriteriaBuilder's methodCreate a predicate for testing the arguments for inequality.See JavaDoc Reference Page...,gt
gt(x, y)CriteriaBuilder's methodCreate a predicate for testing whether the first argument is greater than the second.See JavaDoc Reference Page...,ge
ge(x, y)CriteriaBuilder's methodCreate a predicate for testing whether the first argument is greater than or equal to the second.See JavaDoc Reference Page...,lt
lt(x, y)CriteriaBuilder's methodCreate a predicate for testing whether the first argument is less than the second.See JavaDoc Reference Page...,le
le(x, y)CriteriaBuilder's methodCreate a predicate for testing whether the first argument is less than or equal to the second.See JavaDoc Reference Page...,between
between(v, x, y)CriteriaBuilder's methodCreate a predicate for testing whether the first argument is between the second and third arguments in value.See JavaDoc Reference Page...,isNull
isNull(x)CriteriaBuilder's methodCreate a predicate to test whether the expression is null.See JavaDoc Reference Page..., ...) - Logical expressions (
and
and(x, y)CriteriaBuilder's methodCreate a conjunction of the given boolean expressions.See JavaDoc Reference Page...,or
or(x, y)CriteriaBuilder's methodCreate a disjunction of the given boolean expressions.See JavaDoc Reference Page...,not
not(restriction)CriteriaBuilder's methodCreate a negation of the given restriction.See JavaDoc Reference Page...,isTrue
isTrue(x)CriteriaBuilder's methodCreate a predicate testing for a true value.See JavaDoc Reference Page...).