JPA Criteria Query Expressions

Jakarta Persistence (JPA) Criteria API uses a hierarchy of interfaces to model query conditions and selections, enabling the construction of dynamic, type-safe queries. These interfaces represent the building blocks for defining query logic programmatically.

The hierarchy of the expression interfaces is structured as follows:

Selection<T>                         # Base interface for result items
  └─ Expression<T>                   # Base interface for expressions
       ├─ Predicate                  # Boolean expression (WHERE/HAVING)
       │    └─ CriteriaBuilder.In    # IN predicate
       ├─ Path<X>                    # Navigation path (attributes)
       ├─ ParameterExpression<T>     # Query parameter
       ├─ CriteriaBuilder.Coalesce   # COALESCE expression
       └─ CriteriaBuilder.Case       # CASE expression
            └─ CriteriaBuilder.SimpleCase

Queries may include many different types of expressions and JPA implementations may implement tens and hundreds of classes to manage these different expression types. JPA simplifies expression handling by using a minimal set of reusable interfaces, so many different types of expressions are represented by the same Expression interface. 

Core expressions

The following interfaces represent the fundamental building blocks for query logic:

The root interface for all query expressions, representing any typed value, calculation, or path within the query. Extends Selection to allow using expression as query results.

A boolean expression used to define restrictions in WHERE and HAVING clauses.

An enum defining the logical operators (AND, OR) used to combine multiple predicates.

A navigation path to an attribute value or a referenced entity.

A type-safe representation of a query parameter used for binding runtime values.

Advanced expressions

Additional interfaces represent specific SQL-like expressions:

An expression that returns the first non-null value from a list of arguments.

A predicate used to determine if an expression matches any value in a provided list.

A builder for general conditional logic sequences using when-then-else logic.

A builder for comparing a single expression against specific values.

An enumeration used with the trim function to specify how whitespace should be trimmed (LEADING, TRAILING, or BOTH).

For more details and examples, see the Query Expressions section in the ObjectDB manual.