7
@Query(value = "Select f from Documents f " +
        "RIGHT JOIN f.documentStatus ds " +
        "where f.billingAccount.accountId  in :billingAccountIdList " +
        " and ds.statusCode in :paymentStatuses" +
        " and f.paymentDate < :paymentDate")
List<FinancialDocumentEntity> getFinancialDocumentsOverdue(@Param("billingAccountIdList")List<String> billingAccountIdList,
                                                           @Param("paymentStatuses") List<String> paymentStatuses,
                                                           @Param("paymentDate") Date paymentDate);

I have query like above. It is possible to skip searching param for example @Param("paymentStatuses") in query method if is null or is empty ?

luafanti
  • 321
  • 1
  • 6
  • 18
  • 1
    Possible duplicate of [Spring Data - ignore parameter if it has a null value](https://stackoverflow.com/questions/43780226/spring-data-ignore-parameter-if-it-has-a-null-value) – araknoid Oct 17 '17 at 12:06

2 Answers2

16

Try changing

" and ds.statusCode in :paymentStatuses"

into

" and (:paymentStatuses is null or ds.statusCode in :paymentStatuses)"
Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
6

Try changing

" and ds.statusCode in :paymentStatuses"

into

" and (COALESCE(:paymentStatuses, null) is null or ds.statusCode in :paymentStatuses)"

This solution will work for the empty list, null list, and a list with items 1 or more.

Shivanshu Goyal
  • 500
  • 9
  • 25