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 (including String and Collection operators):
=, <>, <, <=,>, >=, IS [NOT] NULL, [NOT] BETWEEN,
[NOT] LIKE, [NOT] IN, IS [NOT] EMPTY, [NOT] MEMBER [OF] - 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:
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..., nullLiteralnullLiteral(resultClass)CriteriaBuilder's methodCreate an expression for a null literal with the given type.
See JavaDoc Reference Page..., currentDatecurrentDate()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..., typetype()Path's methodCreate an expression corresponding to the type of the path.
See JavaDoc Reference Page...). -
Arithmetic expressions (sumsum(x, y)CriteriaBuilder's methodCreate an expression that returns the sum of its arguments.
See JavaDoc Reference Page..., diffdiff(x, y)CriteriaBuilder's methodCreate an expression that returns the difference between its arguments.
See JavaDoc Reference Page..., prodprod(x, y)CriteriaBuilder's methodCreate an expression that returns the product of its arguments.
See JavaDoc Reference Page..., quotquot(x, y)CriteriaBuilder's methodCreate an expression that returns the quotient of its arguments.
See JavaDoc Reference Page..., modmod(x, y)CriteriaBuilder's methodCreate an expression that returns the modulus of its arguments.
See JavaDoc Reference Page..., absabs(x)CriteriaBuilder's methodCreate an expression that returns the absolute value of its argument.
See JavaDoc Reference Page..., negneg(x)CriteriaBuilder's methodCreate an expression that returns the arithmetic negation of its argument.
See JavaDoc Reference Page..., sqrtsqrt(x)CriteriaBuilder's methodCreate an expression that returns the square root of its argument.
See JavaDoc Reference Page...). -
String expressions (likelike(x, pattern)CriteriaBuilder's methodCreate a predicate for testing whether the expression satisfies the given pattern.
See JavaDoc Reference Page..., lengthlength(x)CriteriaBuilder's methodCreate expression to return length of a string.
See JavaDoc Reference Page..., locatelocate(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..., lowerlower(x)CriteriaBuilder's methodCreate expression for converting a string to lowercase.
See JavaDoc Reference Page..., upperupper(x)CriteriaBuilder's methodCreate expression for converting a string to uppercase.
See JavaDoc Reference Page..., concatconcat(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 (currentDatecurrentDate()CriteriaBuilder's methodCreate expression to return current date.
See JavaDoc Reference Page..., currentTimecurrentTime()CriteriaBuilder's methodCreate expression to return current time.
See JavaDoc Reference Page..., currentTimestampcurrentTimestamp()CriteriaBuilder's methodCreate expression to return current timestamp.
See JavaDoc Reference Page...). -
Collection expressions (isEmptyisEmpty(collection)CriteriaBuilder's methodCreate a predicate that tests whether a collection is empty.
See JavaDoc Reference Page..., isNotEmptyisNotEmpty(collection)CriteriaBuilder's methodCreate a predicate that tests whether a collection is not empty.
See JavaDoc Reference Page..., isMemberisMember(elem, collection)CriteriaBuilder's methodCreate a predicate that tests whether an element is a member of a collection.
See JavaDoc Reference Page..., isNotMemberisNotMember(elem, collection)CriteriaBuilder's methodCreate a predicate that tests whether an element is not a member of a collection.
See JavaDoc Reference Page..., sizesize(collection)CriteriaBuilder's methodCreate an expression that tests the size of a collection.
See JavaDoc Reference Page...). -
Comparison expressions (equalsize(collection)CriteriaBuilder's methodCreate an expression that tests the size of a collection.
See JavaDoc Reference Page..., notEqualnotEqual(x, y)CriteriaBuilder's methodCreate a predicate for testing the arguments for inequality.
See JavaDoc Reference Page..., gtgt(x, y)CriteriaBuilder's methodCreate a predicate for testing whether the first argument is greater than the second.
See JavaDoc Reference Page..., gege(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..., ltlt(x, y)CriteriaBuilder's methodCreate a predicate for testing whether the first argument is less than the second.
See JavaDoc Reference Page..., lele(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..., betweenbetween(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..., isNullisNull(x)CriteriaBuilder's methodCreate a predicate to test whether the expression is null.
See JavaDoc Reference Page..., ...) -
Logical expressions (andand(x, y)CriteriaBuilder's methodCreate a conjunction of the given boolean expressions.
See JavaDoc Reference Page..., oror(x, y)CriteriaBuilder's methodCreate a disjunction of the given boolean expressions.
See JavaDoc Reference Page..., notnot(restriction)CriteriaBuilder's methodCreate a negation of the given restriction.
See JavaDoc Reference Page..., isTrueisTrue(x)CriteriaBuilder's methodCreate a predicate testing for a true value.
See JavaDoc Reference Page...).
This documentation explains how to use JPA in the context of the ObjectDB Object Database but mostly relevant
also for ORM JPA implementations, such as Hibernate (and HQL), EclipseLink, TopLink, OpenJPA and DataNucleus.
ObjectDB is not an ORM JPA implementation but an Object Database (ODBMS) for Java with built in JPA 2 support.
