I try to implement a JPA custom repository.
I have a filter object like this:
public class FilterPatient {
private String surname;
private String
What you are looking for is the Specification pattern which is discussed in relation to Spring Data JPA at the following:
https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/
and which notes with reference to having a query method per query:
Although this approach is really convenient (you don’t even have to write a single line of implementation code to get the queries executed) it has two drawbacks: first, the number of query methods might grow for larger applications because of - and that’s the second point - the queries define a fixed set of criterias. To avoid these two drawbacks, wouldn’t it be cool if you could come up with a set of atomic predicates that you could combine dynamically to build your query?
You can implement the Specification pattern using either the JPA criteria API or using QueryDSL. Using the latter this is as easy as having your repository extend the following interface:
http://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/querydsl/QueryDslPredicateExecutor.html
and adding support for Querydsl to your project. For a Maven project you simply need to add the configuration below to your POM. The plugin will auto generate the Query classes required to construct the predicates and you can then call the following methods of your Repository with any combination of parameters:
Iterable findAll(com.querydsl.core.types.OrderSpecifier>... orders)
Iterable findAll(com.querydsl.core.types.Predicate predicate)
Iterable findAll(com.querydsl.core.types.Predicate predicate, com.querydsl.core.types.OrderSpecifier>... orders)
Page findAll(com.querydsl.core.types.Predicate predicate, Pageable pageable)
Iterable findAll(com.querydsl.core.types.Predicate predicate, Sort sort)
T findOne(com.querydsl.core.types.Predicate predicate)
With this approach then your PatientRepository becomes simply:
PatientRepository extends JpaRepository, QueryDslLPredicateExecutor {
// no query methods needed
}
Note that the Spring Data Gosling release also added support for automatically binding HTTP params to a QueryDSL Predicate so you could also remove your Filter and have Spring Data handle everything end-to-end.
https://spring.io/blog/2015/09/04/what-s-new-in-spring-data-release-gosling#querydsl-web-support
There are some examples here showing 1 query method being called with various parameters:
https://stackoverflow.com/a/26450224/1356423
Maven Setup:
4.0.0
.....
4.1.3
.....
com.querydsl
querydsl-jpa
${querydsl.version}
com.querydsl
querydsl-apt
${querydsl.version}
....
com.mysema.maven
apt-maven-plugin
1.1.3
process
target/generated-sources/java
com.querydsl.apt.jpa.JPAAnnotationProcessor