The main goal of this project is to explore basics of Query By Example
API in
Spring Data
environment.
Reference: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example
-
matching with non null fields
Customer customer = // init pattern fields Example<Customer> customerExample = Example.of(customer); customerRepository.findOne(customerExample)
-
matching with null fields (remember to exclude
id
field - if it stays the result will be empty)ExampleMatcher matcher = matching() .withIncludeNullValues() .withIgnorePaths("id"); // and other fields that we don't want to have null in result Customer customer = // init pattern fields Example<Customer> customerExample = Example.of(customer, matcher); customerRepository.findOne(customerExample)
-
matching with:
- ignoring case
- starts with
- ends with
ExampleMatcher matcher = matching() .withIgnoreCase() .withMatcher("firstName", GenericPropertyMatcher::startsWith) .withMatcher("lastName", GenericPropertyMatcher::endsWith); Customer customer = Customer.builder() .firstName("m") .lastName("wicz") .build(); Example<Customer> customerExample = Example.of(customer, matcher); customerRepository.findOne(customerExample);