I want to have a spring data repository interface that takes two parameters. Is there a way to make it have the following behaviour?
MyObject findByParameter
Try this Kolobok
@FindWithOptionalParams
Iterable<MyObject> findByParameterOneAndParameterTwo( String parameterOne, String parameterTwo);
I'm not sure it is possible with repository methods naming but you can use @Query
like
(:parameterOne is null or parameter1 = :parameterOne) and (:parameterTwo is null or parameter2 = :parameterTwo)
I am not sure if its possible using Repo as a separate class but you can use StringBuilder append query with option parameter. This will definitely work
StringBuilder queryBuilder = new StringBuilder();
queryBuilder.append("select p.name from personDemographic p ");
Boolean flag = true;
if(parameterOne != null){
if(flag){
queryBuilder.append("where condition...");
flag=false;
}
}
if(parameterOne != null){
if(flag){
queryBuilder.append("where condition...");
flag = false;
}else{
queryBuilder.append("and condition...");
}
Query query = entityManager.createQuery(queryBuilder.toString());
One solution that's missing here is Spring Data JPA's Query By Example feature and leverage the ExampleMatcher#ignoreNullValues, which is built exactly to solve this problem. A custom query and query builder are not necessary.
This Spring Data query:
ExampleMatcher matcher = ExampleMatcher.matching().withIgnoreNullValues();
Example<MyObject> exampleQuery = Example.of(new MyObject("foo", null), matcher);
List<MyObject> results = repository.findAll(exampleQuery);
Yields a query that looks like:
select *
from myObject
where parameter1 = "foo"
While the following:
ExampleMatcher matcher = ExampleMatcher.matching().withIgnoreNullValues();
Example<MyObject> exampleQuery = Example.of(new MyObject("foo", "bar"), matcher);
List<MyObject> results = repository.findAll(exampleQuery);
Yields:
select *
from myObject
where parameter1 = "foo"
and parameter2 = "bar"
Very cool!
Note: One thing you'll have to do to your Repository
interface is add the QueryByExample
interface. You can do this either by extending the QueryByExample
interface directly, or implicity via the JpaRepository
:
public interface MyObjectRepository extends JpaRepository<MyObject, Long> {}
Try this one,
@Query(value = "SELECT pr FROM ABCTable pr " +
"WHERE((pr.parameterOne = :parameterOne) or (pr.parameterOne = null and :parameterOne = null)) and
((pr.parameterTwo = :parameterTwo) or (pr.parameterTwo = null and :parameterTwo = null)) ")
List<PaymentRequest> getSomething (@Param("parameterOne") String parameterOne,
@Param("parameterTwo") String parameterTwo);
You could do that too.
Repository:
`MyObject findByParameterOneAndParameterTwo( String parameterOne, String parameterTwo);`
if you pass a null parameterTwo, the generated JPQL will include the IS NULL condition:
`myobject0_.parameterTwo is null`
Ex: repository.findByParameterOneAndParameterTwo("D", null);
Reference: https://www.baeldung.com/spring-data-jpa-null-parameters#query-methods