CriteriaBuilder is the factory of the Jakarta Persistence (JPA) Criteria API. It creates the pieces of a type-safe query: the CriteriaQuery itself, predicates such as equal and greaterThan, expressions, sort orders and aggregate functions. It does not run anything; you pass the finished CriteriaQuery to EntityManager.createQuery(...) to execute it.
You obtain it with entityManager.getCriteriaBuilder(). It is most useful when the filters of a query are decided at runtime, for example a search form where every field is optional.
A criteria query is assembled from a few cooperating types, all in jakarta.persistence.criteria:
| Type | Role |
|---|---|
CriteriaBuilder | Factory for queries, predicates, expressions, orders and functions |
CriteriaQuery<T> | The query being built: select, where, order by, group by |
Root<E> | The entity in the FROM clause; the starting point for attribute paths |
Predicate | A boolean condition used in where or having |
Join<X, Y> | A join to a related entity |
Since Jakarta Persistence 3.0 the package is jakarta.persistence. Older tutorials that import javax.persistence target Java EE and JPA 2.x, and they will not compile with Spring Boot 3 or Hibernate 6.
The entity first:
Now a query for products in a category that cost more than a given price, cheapest first:
Every call on cb returns an object that describes part of the query. The provider (Hibernate) turns the final tree into SQL with bind parameters, so values are never concatenated into the statement.
The real value shows when conditions are optional. Collect predicates in a list and apply only those you need:
Passing an empty array to where returns every row, and multiple predicates in one array are combined with AND. Building the same query with JPQL would need string concatenation.
CriteriaBuilder also covers the rest of SQL. This query counts products per category:
Besides count, you get sum, avg, min, max, coalesce, concat, selectCase and many more. Use product.join("reviews") for joins and cb.construct(ProductSummary.class, ...) to project straight into a DTO or record.
Strings such as "price" are checked only at runtime, so a typo fails when the query runs. The static metamodel fixes this: with the Hibernate annotation processor (hibernate-jpamodelgen) on the compile path, a Product_ class is generated and attributes become compile-time constants.
Now renaming the field breaks the build instead of production. In Spring Data JPA, you usually combine the same builder calls with the Specification interface to keep these conditions reusable.
| Situation | Better choice |
|---|---|
| Fixed query with a few parameters | JPQL or a Spring Data derived or @Query method |
| Many optional filters | Criteria API or Spring Data Specification |
| Complex native SQL, window functions | Native query or a SQL library such as jOOQ |
Criteria code is verbose, so do not use it for static queries.
You get it from an EntityManager, which is not thread-safe. Obtain the builder per unit of work, or from the EntityManagerFactory with getCriteriaBuilder(), which is safe to share.
CriteriaBuilder is the factory that creates parts of a query. CriteriaQuery is the query being assembled from those parts and later executed.
Criteria API the same thing?No. The old org.hibernate.Criteria API was deprecated in Hibernate 5.2 and removed in Hibernate 6. Use the standard JPA Criteria API instead.
Values you pass to methods such as equal become bind parameters, so they are safe. Attribute names given as strings should never come from user input.
CriteriaBuilder builds predicates, expressions and queries as objects, which makes dynamic filtering safe and composable. Create a CriteriaQuery, define the Root, add predicates, then run it with the EntityManager. Use the static metamodel for compile-time checks and prefer JPQL for fixed queries.