CriteriaBuilder is the factory of the JPA Criteria API. You use it to create type-safe queries (CriteriaQuery, CriteriaUpdate, CriteriaDelete) and every piece inside them: predicates, comparisons, functions, aggregates, and sort orders. It lets you build queries from Java objects instead of JPQL strings, so the compiler and your IDE can catch mistakes before runtime.
You get a CriteriaBuilder from an EntityManager or an EntityManagerFactory. Since Jakarta EE 9 the package is jakarta.persistence.criteria (older code uses javax.persistence.criteria).
A JPQL query is a string that the persistence provider parses at runtime. A typo in a field name only fails when that line runs. The Criteria API describes the same query as an object tree, and CriteriaBuilder is the entry point that creates the nodes of that tree.
It has three jobs:
createQuery(), createTupleQuery(), createCriteriaUpdate(), createCriteriaDelete().equal, like, between, and, or, not, sum, count, concat, lower, and many more.asc, desc, construct, tuple.The query objects are not executed by the builder. You pass the finished CriteriaQuery to entityManager.createQuery(...), which returns a normal TypedQuery.
With plain JPA outside a container you can call entityManagerFactory.getCriteriaBuilder() instead. The builder is thread-safe, so a single instance per factory is fine. In Spring you normally inject the EntityManager with @PersistenceContext and call getCriteriaBuilder() in the method that builds the query.
The example below finds all active customers from a given city, sorted by name.
The steps are always the same:
CriteriaQuery for the result type with cb.createQuery(Customer.class).Root with query.from(...). It plays the role of the FROM clause and its alias.Predicate objects with the builder.select, where, orderBy, groupBy, and having on the query.TypedQuery with the entity manager and run it.The Hibernate query for this example is roughly select c from Customer c where c.active = true and c.city = ?1 order by c.name asc.
The string "city" above is still a magic string. To remove it, generate the static metamodel (classes such as Customer_) with the Hibernate JPA Metamodel Generator, which runs as an annotation processor at compile time.
Now renaming the field breaks the build instead of production. This is the main reason to prefer the Criteria API over string-based JPQL for dynamic queries.
| Need | Method | Example |
|---|---|---|
| Equality | equal, notEqual | cb.equal(root.get(Customer_.city), "Pune") |
| Comparison | gt, ge, lt, le, greaterThan, between | cb.between(root.get(Order_.total), 10, 100) |
| Text matching | like, lower, upper, concat | cb.like(cb.lower(root.get(Customer_.name)), "a%") |
| Null checks | isNull, isNotNull | cb.isNull(root.get(Customer_.deletedAt)) |
| Membership | in | cb.in(root.get(Customer_.status)).value("NEW").value("OPEN") |
| Logic | and, or, not, conjunction, disjunction | cb.or(p1, p2) |
| Aggregates | count, sum, avg, min, max | cb.count(root) |
| Conditions | selectCase, coalesce, nullif | cb.coalesce(root.get(Customer_.nickname), "n/a") |
| Ordering | asc, desc | cb.desc(root.get(Customer_.createdAt)) |
The strongest use case is a search form where any filter can be empty. You collect predicates in a list and only add the ones the user filled in.
Passing an empty array to where means no restriction, and several predicates in the array are combined with AND. The values travel as bind parameters, so this approach is safe from SQL injection, unlike string concatenation of JPQL.
For readable results prefer cb.createTupleQuery() or cb.construct(OrderSummary.class, ...) with a record, so you do not index into Object[].
JPA 2.1 added CriteriaUpdate and CriteriaDelete, both created from the builder. They run as bulk statements, bypass the persistence context, and need a transaction.
Use Criteria for dynamic queries whose shape depends on runtime input. For fixed queries, JPQL with @NamedQuery or Spring Data repository methods is shorter and easier to read. Spring Data JPA wraps this API in Specification, which gives you the builder inside a lambda. Jakarta Persistence 3.2 and Hibernate 6 also add more helper methods to the builder, so check your provider version before relying on newer overloads.
Yes. The builder obtained from an EntityManagerFactory can be shared between threads. The CriteriaQuery objects you build with it are not meant to be shared while you are still modifying them.
CriteriaBuilder is the factory that creates queries, predicates, and expressions. CriteriaQuery is one specific query that you fill with a root, selection, and conditions.
No. Both end up as the same SQL and the same provider work. The benefit is type safety and easy dynamic composition, not speed.
Jakarta Persistence 3.0 renamed the packages to jakarta.persistence. Update the imports and use a provider such as Hibernate 6 or later.
CriteriaBuilder is the factory behind the JPA Criteria API. Get it from the EntityManager, use it to create a CriteriaQuery, a Root, and Predicate objects, and add the static metamodel for compile-time checks. It shines for dynamic filters, aggregates, and bulk updates, while simple fixed queries are usually clearer in JPQL.