The Criteria API in JPA is a programmatic, object-oriented way to build queries with Java objects instead of JPQL strings. Because queries are assembled from typed builder calls, mistakes in attribute names or types can be caught at compile time when you pair the API with the static metamodel, and queries with optional filters are far easier to compose than by concatenating strings.
Since Jakarta EE 9 the API lives in the jakarta.persistence.criteria package (older versions used javax.persistence.criteria). The code below targets Jakarta Persistence 3.x with Hibernate 6 or a comparable provider.
Every criteria query is built from a small set of types:
| Type | Role |
|---|---|
CriteriaBuilder | Factory for queries, predicates, expressions and functions such as count or lower. |
CriteriaQuery<T> | The query definition, where T is the result type. |
Root<E> | The entity in the FROM clause. |
Predicate | A condition used in where, having or joins. |
Path<X> | A navigable reference to an attribute, such as root.get("price"). |
TypedQuery<T> | The executable query created from a CriteriaQuery. |
You obtain the CriteriaBuilder from an EntityManager (or an EntityManagerFactory) and then describe the query step by step. Nothing runs against the database until you call getResultList() or getSingleResult() on the TypedQuery.
The following example selects all products that cost more than a given amount, ordered by name.
The equivalent JPQL is select p from Product p where p.price > :minPrice order by p.name. The literal value is passed as a bound parameter by the provider, so the query is safe from SQL injection.
The example above still uses the string "price", so a typo only fails at runtime. The JPA static metamodel removes that weakness. An annotation processor (such as hibernate-jpamodelgen) generates a class with a trailing underscore for every entity, for example Product_.
Now renaming or removing the price field breaks compilation instead of breaking production. The generated attributes also carry the Java type, so passing a String where a BigDecimal is expected is rejected by the compiler.
Dynamic filtering is where the Criteria API earns its place. You collect predicates in a list and apply only the ones the caller supplied.
Passing an empty array to where means no restriction, and multiple predicates in one call are combined with AND. Use cb.or(...) when you need alternatives.
Joins are created from the root, and aggregate functions come from the builder. This query counts products per category.
A Tuple query returns several columns without a dedicated class. You can also use cb.construct(ProductSummary.class, ...) to build a DTO directly, or select a single attribute with a result type such as String.
| Aspect | Criteria API | JPQL | Native SQL |
|---|---|---|---|
| Checked at compile time | Yes, with the metamodel | No | No |
| Dynamic conditions | Natural | String concatenation | String concatenation |
| Readability | Verbose | Concise | Concise |
| Database-specific features | Limited | Limited | Full |
Choose JPQL (or named queries) for fixed, simple queries where readability matters, and the Criteria API when the shape of the query changes at runtime. Use native SQL for vendor-specific features such as window functions that your provider does not expose. Since JPA 3.1 and Hibernate 6 you can also use CriteriaUpdate and CriteriaDelete for bulk changes, and Spring Data JPA's Specification interface is a thin wrapper around the same API.
No. Both are translated into the same SQL by the provider. Criteria queries add a little object-building overhead, but it is negligible next to the database call.
It is optional, but without it you lose most of the type safety because attribute names are plain strings. Generating the metamodel at build time is cheap and recommended.
Call setFirstResult(offset) and setMaxResults(pageSize) on the TypedQuery before executing it, and add a stable orderBy so pages are consistent.
Hibernate implements the standard Jakarta Persistence API. Hibernate 6 also offers extra features such as HibernateCriteriaBuilder, but the portable code above works with any compliant provider.
The Criteria API builds JPA queries from typed Java objects. Combine it with the static metamodel for compile-time checks, collect predicates in a list for dynamic filters, and prefer JPQL for static queries that are easier to read. Remember the package is jakarta.persistence.criteria on current versions.