The Specification interface in Spring Data JPA models one reusable condition of a query. Its single method, toPredicate, receives the JPA Criteria API objects Root, CriteriaQuery and CriteriaBuilder and returns a Predicate that becomes part of the WHERE clause. Because specifications can be combined with and, or and not, they are the standard way to build dynamic filters, such as a search form with optional fields, without string-built JPQL.
Note that Specification belongs to Spring Data JPA, not to the JPA standard itself. It is a thin, composable layer over the JPA Criteria API (jakarta.persistence.criteria in Spring Boot 3 and later).
The interface is functional, so a lambda is enough:
Root<T> is the entity in the FROM clause. You navigate attributes and joins from it.CriteriaQuery<?> represents the whole query. You can set distinct, ordering or grouping on it.CriteriaBuilder creates predicates such as equal, like, between and and.The interface also provides default methods and, or and not, plus the static factory where, so conditions chain fluently.
| Need | Derived query methods | @Query strings | Specifications |
|---|---|---|---|
| Fixed filter | Good | Good | Overkill |
| Many optional filters | Method explosion | Messy string concatenation | Compose only what is set |
| Reuse a condition | Not possible | Copy and paste | Share one static method |
| Refactoring safety | Checked at startup | Checked at startup | Better with the static metamodel |
Specifications avoid SQL injection because values are bound as parameters by the persistence provider, not concatenated into the query.
Place reusable conditions in a small utility class. Returning null from toPredicate means "no restriction", which keeps optional filters simple.
Product_ is the static metamodel class generated by Hibernate's hibernate-jpamodelgen annotation processor. Using it instead of string names such as root.get("price") makes attribute names and types checked at compile time. String names also work, but a typo only fails at runtime.
User input in a like pattern should have % and _ escaped if users must not control wildcards.
The repository must extend JpaSpecificationExecutor, which adds findAll(spec), findAll(spec, pageable), findAll(spec, sort), findOne(spec), count(spec) and exists(spec).
A null specification passed to and is ignored, so each optional filter only applies when a value was supplied. In recent Spring Data JPA releases, Specification.where(null) style calls are being replaced by explicit factories such as Specification.unrestricted(), so check the Javadoc of the version you use.
Specifications can join associations. When you join a collection, rows are duplicated, so set distinct:
With pagination, Spring Data also runs a count query using the same specification. Calling methods such as root.fetch(...) inside toPredicate breaks the count query, because a count has no fetched entities. Guard it with if (query.getResultType() != Long.class) or use an entity graph instead.
No. It is a Spring Data JPA type that wraps the standard Criteria API. The Criteria API itself is part of Jakarta Persistence.
Call spec1.or(spec2). You can mix and, or and not to build any boolean expression.
Not inherently. They produce the same kind of query. The benefit is composition and maintainability, so measure performance with your database's query plan.
Specifications with JpaSpecificationExecutor return entities. For DTO results, use a findBy fluent query with a projection in current Spring Data versions, or a custom repository method.
A Specification is a small, composable condition built on the JPA Criteria API. Write each filter as a static method, return null for unused filters, combine them with and and or, and execute them through JpaSpecificationExecutor. Prefer the generated static metamodel for compile-time safety, and be careful with joins and fetches when paging.