CriteriaBuilder is the central factory of the Jakarta Persistence (JPA) Criteria API. You use it to build type-safe queries in Java code instead of writing JPQL strings: it creates the query objects, the predicates (conditions), the expressions, the sort orders and the aggregate functions. Because queries are assembled from objects, they can be composed dynamically at runtime and checked by the compiler.
You obtain a CriteriaBuilder from the EntityManager, create a CriteriaQuery, describe the query with it, and run the result through EntityManager.createQuery.
CriteriaBuilderCriteriaBuilder can createCriteriaBuilderSince JPA 3.0 the packages use the jakarta.persistence prefix (they were javax.persistence in JPA 2.x and in Spring Boot 2).
This is the Criteria equivalent of SELECT e FROM Employee e WHERE e.department = :dept AND e.salary > :min ORDER BY e.name:
Root represents the entity in the FROM clause, cb.equal and cb.greaterThan build predicates, and where with several predicates combines them with AND.
CriteriaBuilder can create| Category | Methods |
|---|---|
| Queries | createQuery, createTupleQuery, createCriteriaUpdate, createCriteriaDelete |
| Comparison | equal, notEqual, gt, ge, lt, le, between, like, isNull |
| Logical | and, or, not, conjunction, disjunction |
| Aggregates | count, sum, avg, min, max |
| Functions | lower, upper, concat, length, coalesce, function |
| Ordering | asc, desc |
| Subqueries | created from the CriteriaQuery, combined with exists, in |
Since JPA 3.2 the Criteria API also gained additional operations, but the methods above are available in every supported version.
Search screens with optional filters are where the Criteria API pays off, because the predicate list is built from whatever the user supplied:
All values are passed as parameters by the provider, so this style is safe from SQL injection, unlike string concatenation.
For joins use e.join("projects", JoinType.LEFT). For typed projections prefer cb.construct(Dto.class, ...) or a Tuple query over Object[].
Strings such as "salary" are checked only at runtime. Generating the static metamodel (Hibernate's hibernate-jpamodelgen annotation processor) produces classes like Employee_, so you can write e.get(Employee_.salary) and get compile-time checking and refactoring support.
| Approach | Strength | Weakness |
|---|---|---|
| JPQL strings | Short and readable | Dynamic queries need string building |
Criteria API with CriteriaBuilder | Standard, dynamic, type-safe with metamodel | Verbose |
Spring Data Specification | Reusable predicates on top of Criteria | Spring-specific |
| Querydsl or jOOQ | Fluent and concise | Extra dependency |
Spring Data JPA exposes the same machinery through JpaSpecificationExecutor and Specification, where the lambda receives root, query and the CriteriaBuilder.
CriteriaBuilder thread safe?A CriteriaBuilder obtained from an EntityManagerFactory is thread safe, while one from an EntityManager is tied to that manager's usage. Treat the built CriteriaQuery objects as per-request and do not share them across threads.
Use JPQL or Spring Data derived queries for fixed queries. Use Criteria when the filter, sort or join structure depends on runtime input.
Values passed to cb.equal, cb.like and similar methods become query parameters. Injection is still possible if you pass user input to cb.function as an SQL fragment name or build sort fields from raw strings, so validate those.
Yes, with createCriteriaUpdate and createCriteriaDelete. These are bulk operations that bypass the persistence context.
CriteriaBuilder is the JPA factory for building queries as objects. It shines for dynamic filters, aggregates and reusable predicates, and it becomes type-safe when combined with the generated static metamodel. For fixed queries a JPQL string or a Spring Data repository method is usually simpler.