The Criteria API in JPA (now Jakarta Persistence) is a type-safe, programmatic way to build queries as Java objects instead of JPQL strings. Its significance is that queries are checked by the compiler, can be assembled dynamically at runtime, and are easier to refactor than string concatenation.
You use it when the filters, sorting or joins of a query depend on user input, such as a search form with optional fields. For fixed queries, JPQL or Spring Data derived queries are usually shorter.
The API lives in jakarta.persistence.criteria (javax.persistence.criteria before Jakarta EE 9 and Spring Boot 3). Its main types are:
| Type | Role |
|---|---|
CriteriaBuilder | Factory for queries, predicates and expressions, obtained from EntityManager.getCriteriaBuilder() |
CriteriaQuery<T> | The query being built, with result type T |
Root<E> | The entity in the FROM clause |
Predicate | A condition for WHERE or HAVING |
Join | A join to a related entity |
Path | A reference to an attribute of a root or join |
The basic style shown below works across Jakarta Persistence versions, including 3.2 in Jakarta EE 11.
Assume a Product entity with name, price and category.
This is equivalent to select p from Product p where p.price >= :minPrice order by p.price desc. The string "price" is still checked only at runtime, which leads to the next section.
To get compile-time checking of attribute names, generate the static metamodel. Add the hibernate-jpamodelgen annotation processor (or the Hibernate 7 equivalent, hibernate-processor) to your build, and it creates classes such as Product_.
If you rename the field price, the code no longer compiles, instead of failing in production. This is the main practical advantage over string-based queries.
The strongest use case is combining optional conditions. Collect predicates in a list and apply them at the end.
Doing this with JPQL means gluing strings together and tracking parameters by hand, which is error-prone and invites injection mistakes. The Criteria API always binds values as parameters.
You can join associations and select aggregates or DTOs.
With cb.construct(OrderSummary.class, ...) you can map rows straight to a DTO class or record. Remember to add distinct or fetch joins deliberately to avoid duplicate rows and N+1 queries.
You rarely write the boilerplate by hand in Spring applications. Spring Data's Specification wraps the Criteria API in reusable pieces.
Specifications compose with and, or and not, and they work with paging and sorting.
| Aspect | Criteria API | JPQL | Querydsl or jOOQ |
|---|---|---|---|
| Compile-time checks | Yes, with the metamodel | No | Yes |
| Readability | Verbose | Best for simple queries | Fluent and compact |
| Dynamic queries | Strong | Weak | Strong |
| Extra dependency | Metamodel generator | None | Library |
Use JPQL for static queries, Criteria or Specifications for dynamic filters in plain JPA, and Querydsl or jOOQ when you want a more readable fluent style.
No. Both are translated to SQL by the provider. Criteria queries may cost slightly more to build, but the executed SQL is comparable.
It is optional, but without it you pass attribute names as strings and lose compile-time safety.
Yes. Use CriteriaUpdate and CriteriaDelete for bulk operations, which bypass entity lifecycle callbacks.
Yes. It is part of the Jakarta Persistence standard and underlies Spring Data Specifications.
The Criteria API gives you type-safe, dynamic, parameterised queries built from Java objects. Pair it with the static metamodel for compile-time checks, use Specifications in Spring Data, and fall back to JPQL for simple fixed queries.