13

I'm trying to understand how to use Spring Data's Query by Example capabilities, and am struggling to understand how to use ExampleMatcher and its various with* methods. Classic examples of the matcher in use includes snippets like this:

Person person = new Person();                          
person.setFirstname("Dave");                           

ExampleMatcher matcher = ExampleMatcher.matching()     
  .withIgnorePaths("lastname")                         
  .withIncludeNullValues()                             
  .withStringMatcherEnding();                          

Example<Person> example = Example.of(person, matcher);

For some reason, I just can't wrap my brain around this DSL. Let's take the Person example from the docs. Let's say a Person entity looks like this:

// Pseudo code; omitting JPA annotations for this example
public class Person {
    private String firstName;
    private String lastName;
    private Date dob;
    private Long score;

    // Getters, setters & constructor omitted
}

Can anyone show me an example of how to construct an ExampleMatcher that would allow me to find Person records meeting the following criteria:

  • First name starts with "Sme"; and
  • Last name is less than 10 characters long; and
  • Date of birth is before 1970-01-01; and
  • Score is between 10 and 20, inclusively

If any of these criteria aren't possible with ExampleMatcher, that's fine, but can someone show me which ones are or explain what methods might get me close to what I'm looking for?

halfer
  • 19,824
  • 17
  • 99
  • 186
smeeb
  • 27,777
  • 57
  • 250
  • 447
  • 3
    Check the Limitation section in the document you specified, It says Only supports starts/contains/ends/regex matching for strings and exact matching for other property types – pvpkiran Feb 02 '18 at 09:02
  • Thanks @pvpkiran (+1) so in that case can you show me how I would do the following two things: **(1)** filter for first names that start with "Sme", and **(2)** filter for scores that are exactly 50? Thanks again! – smeeb Feb 02 '18 at 12:26
  • Official Documentation will be helpful: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example.matchers – Dusman May 25 '20 at 10:58
  • @pvpkiran In order to search records which have firstname given as an input in a string say s and score given as an input in a Integer i how the matcher can be written ? Basically I want as for any string for any field in person class how the search example can be created. Using Example.of(person, ExampleMatcher.matching()) – thealchemist Nov 15 '21 at 12:46

1 Answers1

24

You can get records with firstName starting with "Sme" and score=50

Person person = new Person();
person.setFirstName("Sme");
person.setScore(50L);
ExampleMatcher matcher = ExampleMatcher.matching()
    .withMatcher("firstName", startsWith())
    .withMatcher("score", exact());

Example<History> example = Example.of(person, matcher);
personRepository.findAll(example)
pvpkiran
  • 25,582
  • 8
  • 87
  • 134
  • 1
    Use this code for new version ExampleMatcher matcher = ExampleMatcher.matching() .withMatcher("firstName", new GenericPropertyMatcher().startsWith()) .withMatcher("score", new GenericPropertyMatcher().exact()); – ThinkTank Feb 18 '19 at 11:08
  • I am example matcher to filter some values, but how to use this to filter a column with a list. Means as long as any row contains the column value in the list. I can use @Query but using with other fields checking null would be difficult to maintain – Satish Patro Oct 08 '19 at 08:23
  • @ThinkTank, import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.startsWith; – Satish Patro Oct 08 '19 at 09:03
  • 7
    any idea how to match a collection, kind of $in? – Next Developer Mar 23 '20 at 15:02
  • 1
    HI @NextDeveloper did you find the solution for $in clause? if you did, can you please post the solution – Gaali Prabhakar Jun 21 '21 at 10:36