2017-01-25 133 views
2

我有對象FilterData。通過實例查詢彈簧數據

@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; 
    } 
} 

庫:

@RepositoryRestResource 
public interface FilterDataRepository extends JpaRepository<FilterData, Long> {} 

依賴關係:

<?xml version="1.0" encoding="UTF-8"?> 
<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"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.example</groupId> 
    <artifactId>my-project</artifactId> 
    <version>1.0</version> 
    <packaging>jar</packaging> 

    <name>Project Demo</name> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.4.2.RELEASE</version> 
     <relativePath/> <!-- lookup parent from repository --> 
    </parent> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 

     <java.version>1.8</java.version> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-jpa</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.h2database</groupId> 
      <artifactId>h2</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-rest</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.projectlombok</groupId> 
      <artifactId>lombok</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 


</project> 

測試:

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) -> { 

      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<FilterData> 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 
     }; 
    } 
} 

當我嘗試「的findAll()」我得到的所有對象,但是當我嘗試從例子中查詢我什麼都得不到。

當我想我是以下spring data jpa query by example

什麼建議嗎?

回答

2

更新您匹配到這一點:

ExampleMatcher matcher = ExampleMatcher.matching().withIncludeNullValues().withIgnorePaths("id","imsi", "mnc"); 

你沒有得到任何東西,因爲匹配與所有現場比賽查詢但你有興趣只能由MCC,你需要告訴忽略其他領域。

+0

我有一個類似的解決方案去了,但工作,謝謝。我的解決方案:'ExampleMatcher.matching()。withIgnoreNullValues()。withIgnorePaths(「id」)' –

1

問題是由於存儲庫正在查找JPA實體ID,在這種情況下,它正在查找ID = 0。此外,未設置值爲null,需要在匹配器中忽略。

你只需要忽略標識字段

ExampleMatcher.matching().withIgnoreNullValues() 
    .withIgnorePaths("id"); 
+1

我剛剛從includeNullValues更改爲忽略它,它工作,謝謝! –