In Spring Data JPA there is no class called "Criteria interface" that you filter with directly. Dynamic filtering is done with the standard JPA Criteria API (CriteriaBuilder, CriteriaQuery, Root, Predicate), which Spring Data wraps in the Specification interface. A specification turns one filter condition into a reusable object that you combine with others and pass to a repository.
The legacy Hibernate org.hibernate.Criteria interface also exists in old code, but it was deprecated in Hibernate 5.2 and removed in Hibernate 6, so new Spring Boot 3 projects should not use it.
The names are easy to confuse, so here is how each fits into filtering:
| Type | Package | Role in filtering |
|---|---|---|
CriteriaBuilder | jakarta.persistence.criteria | Creates predicates such as equal, like, between |
CriteriaQuery<T> | jakarta.persistence.criteria | The query being built |
Root<T> | jakarta.persistence.criteria | The entity being queried; gives access to its attributes |
Predicate | jakarta.persistence.criteria | One boolean condition |
Specification<T> | org.springframework.data.jpa.domain | Spring Data wrapper that returns a Predicate |
org.hibernate.Criteria | Hibernate (removed in 6) | Legacy API; do not use |
All imports in modern Spring Boot 3 projects use jakarta.persistence, not javax.persistence.
A repository gains filtering support by also extending JpaSpecificationExecutor:
JpaSpecificationExecutor adds findAll(Specification), findAll(Specification, Pageable), findAll(Specification, Sort), findOne, count and exists overloads.
Each specification is a small lambda that receives the Root, the CriteriaQuery and the CriteriaBuilder and returns a Predicate. Put them in one class so the conditions can be named and tested:
Each method describes only its own condition. Nothing runs until the repository executes the combined specification.
A service can add only the filters the user supplied, then call the repository:
Specification.allOf combines every specification with AND (an empty list matches all rows), and anyOf combines them with OR. You can also chain and(...), or(...) and not(...). Sorting and pagination come from the Pageable argument, and Spring Data issues the count query for the page automatically.
A request such as GET /products?category=books&maxPrice=20&page=0&size=10&sort=price,asc produces a query filtered by category and price, sorted ascending, returning ten rows.
"category") are checked at runtime. The generated static metamodel (Product_.category) catches typos at compile time.query.distinct(true) and fetch joins with care, and remember that a fetch join inside a paged query causes in-memory paging.findByCategoryAndPriceLessThanEqual is simpler. Use specifications only when conditions vary.Not as a single type. The relevant types are the JPA Criteria API classes and Spring Data's Specification. The name usually refers to the old Hibernate Criteria, which is gone in Hibernate 6.
Specifications need no extra build step and fit simple dynamic filters. Querydsl generates query types and reads more naturally for complex queries, at the cost of a code generator.
Call root.join("reviews") inside the specification and add a predicate on the join. Keep paging and duplicates in mind as described above.
@Query methods?Not in the same call. Use a specification for dynamic filters and keep @Query methods for fixed queries.
Dynamic filtering in Spring Data JPA means the JPA Criteria API wrapped in Specification. Extend JpaSpecificationExecutor, write one small specification per condition, combine them with allOf, and pass a Pageable. Avoid the removed Hibernate Criteria and use the metamodel to keep attribute names type-safe.