Use CriteriaBuilder to create a CriteriaQuery, collect a List<Predicate> for each filter that is present, pass them to where(...), and execute the query with EntityManager.createQuery. Because the predicates are built at runtime and values are bound as parameters, you can add or skip conditions per request without string concatenation.
The Criteria API is part of Jakarta Persistence (JPA 3.x, package jakarta.persistence.criteria). The examples below work with Hibernate 6 and Spring Boot 3, and with any other compliant JPA provider.
| Type | Role |
|---|---|
CriteriaBuilder | Factory for queries, predicates, expressions and functions |
CriteriaQuery<T> | The query definition, typed by its result |
Root<E> | The entity in the FROM clause |
Predicate | A condition for WHERE or HAVING |
Join<X, Y> | A join to a related entity |
TypedQuery<T> | The executable query with paging and parameters |
You get the builder with entityManager.getCriteriaBuilder(). Nothing runs until you call createQuery(criteriaQuery) and then getResultList().
With an empty predicate list, where() with no arguments leaves the query unrestricted, so the same method serves "no filters" and "all filters". Hibernate produces a parameterised statement, so user input never becomes SQL text. Escape % and _ in the search text if you need literal matching.
cb.and(...) and cb.or(...) accept several predicates. Passing a list to where joins them with AND.
For IN lists use product.get("category").in(categories). Check that a collection is not empty first, since an empty IN can fail or behave differently across databases.
To avoid the N+1 select problem when you also need the association loaded, use order.fetch("customer") on an entity query. Do not combine collection fetch joins with paging, because Hibernate then paginates in memory.
For the total, build a second query with cb.createQuery(Long.class), select cb.count(root) and reuse the same predicates. Build predicates in a helper method that takes a Root, so both queries share the logic.
Return only the columns you need with cb.construct (a DTO or Java record with a matching constructor) or a tuple.
For aggregates use cb.sum, cb.avg and cb.count, together with cq.groupBy(...) and cq.having(...).
Strings such as "price" are checked only at runtime. Generate the static metamodel with the hibernate-jpamodelgen annotation processor and use Product_.price instead.
A rename then becomes a compile error. In Spring Data JPA, you can wrap the same lambdas in Specification<Product> to get reusable, composable filters on repositories.
Use Criteria when the shape of the query changes at runtime. Use JPQL or @Query when the query is fixed, because it is shorter and easier to read.
No. Performance depends on the generated SQL and the indexes. Criteria queries are built on each call, which costs a little CPU, but the database time dominates.
Run a separate CriteriaQuery<Long> with cb.count(root) and the same predicates, without ordering.
Use cb.equal, cb.like and similar methods with values. They are bound as parameters. Never concatenate input into JPQL.
Create the builder, query and root, add a predicate for each present filter, apply them with where, and execute with TypedQuery. Use joins, paging, projections and the generated metamodel as needed, and keep predicate construction in a reusable helper.