CriteriaQuery is the central interface of the JPA Criteria API. It lets you build a database query as a tree of Java objects instead of a JPQL string, so the query is checked by the compiler, composes cleanly at runtime, and avoids string concatenation. It lives in jakarta.persistence.criteria (formerly javax.persistence.criteria before Jakarta Persistence 3.0).
You get a CriteriaBuilder from the EntityManager, ask it for a CriteriaQuery<T>, describe the query with from, select, where and orderBy, and run it through entityManager.createQuery(...).
Four types work together:
| Type | Role |
|---|---|
CriteriaBuilder | Factory for queries, predicates, expressions and functions |
CriteriaQuery<T> | The query itself, with result type T |
Root<E> | The entity in the FROM clause, used to reach attributes |
Predicate | A condition for WHERE, HAVING or joins |
Sibling interfaces CriteriaUpdate<T> and CriteriaDelete<T> cover bulk updates and deletes. CriteriaQuery itself is only for selects.
Assume this entity:
Find employees in a department, highest salary first:
This is equivalent to the JPQL SELECT e FROM Employee e WHERE e.department = :dept ORDER BY e.salary DESC. The value dept is sent as a bind parameter, so the query is safe against SQL injection.
The main reason to use CriteriaQuery is dynamic filtering, where the set of conditions depends on user input. Collect predicates in a list and apply them together:
Building the same query with JPQL would need string concatenation and manual parameter tracking, which is error-prone.
Change the result type to select columns, tuples or aggregates.
For type-safe rows prefer CriteriaQuery<Tuple> with cb.createTupleQuery(), or a DTO via cb.construct(DepartmentStat.class, ...). Joins use root.join("projects", JoinType.LEFT), and root.fetch(...) loads associations eagerly in the same query to avoid N+1 selects.
For pagination, set limits on the TypedQuery, not on the criteria:
String attribute names such as "department" are only checked at runtime. The JPA static metamodel generates classes like Employee_ (for example with Hibernate's hibernate-jpamodelgen annotation processor), so attributes are checked at compile time:
Renaming a field then breaks the build instead of failing in production.
| Approach | Strength | Weakness |
|---|---|---|
| JPQL | Short and readable | Strings, no compile-time check, awkward for dynamic filters |
CriteriaQuery | Type-safe, composable, dynamic | Verbose |
Spring Data Specification | Reuses Criteria with a cleaner API | Needs Spring Data JPA |
| Querydsl or jOOQ | Fluent, very readable | Extra dependency |
Use JPQL for fixed queries and the Criteria API when the query shape changes at runtime. Recent Hibernate versions also provide a typed query API that adds some of this convenience, but CriteriaQuery remains the portable standard across JPA providers.
CriteriaBuilder and CriteriaQuery?CriteriaBuilder is a factory for the building blocks (queries, predicates, functions). CriteriaQuery is the specific query being assembled.
No. Both are translated to SQL by the provider, so performance depends on the generated SQL, indexes and fetching strategy, not on the API.
CriteriaQuery for updates and deletes?Not directly. Use CriteriaUpdate and CriteriaDelete, which also come from CriteriaBuilder.
ClassCastException or a Tuple error?The result type passed to createQuery must match what you select. Use Object[].class, Tuple.class or a constructor expression for multi-column selects.
CriteriaQuery builds JPA select queries as objects, which gives you dynamic, composable and compile-time-checkable queries. Obtain it from CriteriaBuilder, define the Root, add Predicate conditions and run it with EntityManager.createQuery. Pair it with the static metamodel for type safety, and keep JPQL for simple fixed queries.