{commons}@data-commons::page$query-by-example.adoc
In Spring Data JPA, you can use Query by Example with Repositories, as shown in the following example:
public interface PersonRepository extends JpaRepository<Person, String> { … }
public class PersonService {
@Autowired PersonRepository personRepository;
public List<Person> findPeople(Person probe) {
return personRepository.findAll(Example.of(probe));
}
}
Note
|
Currently, only SingularAttribute properties can be used for property matching.
|
The property specifier accepts property names (such as firstname
and lastname
). You can navigate by chaining properties together with dots (address.city
). You can also tune it with matching options and case sensitivity.
The following table shows the various StringMatcher
options that you can use and the result of using them on a field named firstname
:
StringMatcher
options
Matching | Logical result |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Note
|
Regex-matching is not supported by JPA. |