devxlogo

How to Use the Spring Example API

Spring Query By Example pattern

Spring Data’s Query By Example (QBE) API is still one of the simplest ways to build dynamic, type-safe queries without hand-writing JPQL or a Criteria chain. You populate a probe entity with the fields you care about, wrap it in an Example, and let the repository translate it into a query at runtime. In 2026 this is especially useful for admin search pages, filter bars, and lightweight CRUD microservices where Specifications feel heavy.

Start by defining a probe (an instance of the entity class):

User user = new User();
user.setName("Ana");
user.setEmail("[email protected]");

Wrap the probe in an Example:

Example<User> userExample = Example.of(user);

Define a repository that extends QueryByExampleExecutor:

@Repository
public interface UserRepository
    extends JpaRepository<User, Long>, QueryByExampleExecutor<User> { }

Call one of the executor methods. exists(), findAll(), findOne(), and count() are all supported:

public boolean existsUser(User user) {
    Example<User> userExample = Example.of(user);
    return userRepository.exists(userExample);
}

2026 notes and tradeoffs

Use an ExampleMatcher when you want case-insensitive matching, partial strings, or null handling. QBE still does not support OR predicates, nested properties on collection associations, or ranges (greater-than, between). For anything richer than equality on scalar fields, reach for a Spring Data Specification or the newer @Query projections in Spring Data JPA 3.x.

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  ChatGPT Uninstalls Jumped 295% After OpenAI's Military Deal

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.