title: Spring data query by Example
body: <p>I have object FilterData.</p>
  
  <pre><code>@Entity
  @Getter
  @Setter
  @ToString
  @NoArgsConstructor(access = AccessLevel.PUBLIC)
  public class FilterData implements Serializable {
  
      @Id
      @GeneratedValue(strategy= GenerationType.IDENTITY)
      private long id;
  
      @Size(max = 20)
      private String imsi;
  
      @Size(max = 10)
      private String mcc;
  
      @Size(max = 10)
      private String mnc;
  
      public FilterData(String imsi, String mcc, String mnc){
          this.imsi = imsi;
          this.mcc = mcc;
          this.mnc = mnc;
      }
  }
  </code></pre>
  
  <p>repository:</p>
  
  <pre><code>@RepositoryRestResource
  public interface FilterDataRepository extends JpaRepository&lt;FilterData, Long&gt; {}
  </code></pre>
  
  <p>dependencies: </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;com.example&lt;/groupId&gt;
      &lt;artifactId&gt;my-project&lt;/artifactId&gt;
      &lt;version&gt;1.0&lt;/version&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
  
      &lt;name&gt;Project Demo&lt;/name&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.4.2.RELEASE&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
  
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
  
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.h2database&lt;/groupId&gt;
              &lt;artifactId&gt;h2&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-rest&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
              &lt;artifactId&gt;lombok&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  
  &lt;/project&gt;
  </code></pre>
  
  <p>Testing:</p>
  
  <pre><code>package com.example;
  
  import com.example.orm.FilterData;
  import com.example.repository.FilterDataRepository;
  import org.springframework.boot.CommandLineRunner;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.context.annotation.Bean;
  import org.springframework.data.domain.Example;
  import org.springframework.data.domain.ExampleMatcher;
  import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  
  @EnableJpaRepositories
  @SpringBootApplication
  public class DemoApplication {
  
      public static void main(String[] args) {
          SpringApplication.run(DemoApplication.class, args);
      }
  
      @Bean
      public CommandLineRunner demo(FilterDataRepository filterDataRepository){
          return (args) -&gt; {
  
              FilterData fd1 = new FilterData("12345", "219", "01");
              FilterData fd2 = new FilterData("12345", "219", "02");
              FilterData fd3 = new FilterData("56789", "218", "10");
              FilterData fd4 = new FilterData("56789", "222", "02");
              FilterData fd5 = new FilterData("10010", "443", "44");
  
              filterDataRepository.save(fd1);
              filterDataRepository.save(fd2);
              filterDataRepository.save(fd3);
              filterDataRepository.save(fd4);
              filterDataRepository.save(fd5);
  
  
              FilterData fd = new FilterData();
              fd.setMcc("218");
  
              ExampleMatcher matcher = ExampleMatcher.matching()
                      .withIncludeNullValues();
  
              Example&lt;FilterData&gt; ex = Example.of(fd, matcher);
              System.out.println(filterDataRepository.findAll()); // prints all
  
              System.out.println(filterDataRepository.count(ex)); // 0
              System.out.println(filterDataRepository.findOne(ex)); // null
              System.out.println(filterDataRepository.findAll(ex)); // [] empty
          };
      }
  }
  </code></pre>
  
  <p>When I try “findAll()” I get all objects, but when I try query from example I can't get anything.</p>
  
  <p>When I tried i was following <a href="http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example" rel="noreferrer">spring data jpa query by example</a></p>
  
  <p>Any suggestion?</p>