I have object 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;
}
}
repository:
@RepositoryRestResource
public interface FilterDataRepository extends JpaRepository<FilterData, Long> {}
dependencies:
<?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>
Testing:
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
};
}
}
When I try “findAll()” I get all objects, but when I try query from example I can't get anything.
When I tried i was following spring data jpa query by example
Any suggestion?
Update your matcher to this:
ExampleMatcher matcher = ExampleMatcher.matching().withIncludeNullValues().withIgnorePaths("id","imsi", "mnc");
You are not getting anything because matcher is querying with all field match but as you are interested to only by mcc, you need to tell to ignore other fields.
The issue is due to the repository looking for the JPA entity ID, in this case it is looking for ID = 0. Also, the un-set values are null and need to be ignored in the matcher.
You only need to ignore the identity field
ExampleMatcher.matching().withIgnoreNullValues()
.withIgnorePaths("id");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With