How to skip @Param in @Query if is null or empty in Spring Data JPA

后端 未结 2 1227
逝去的感伤
逝去的感伤 2021-01-18 08:39
@Query(value = \"Select f from Documents f \" +
        \"RIGHT JOIN f.documentStatus ds \" +
        \"where f.billingAccount.accountId  in :billingAccountIdList \"         


        
相关标签:
2条回答
  • 2021-01-18 08:58

    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.

    0 讨论(0)
  • 2021-01-18 09:11

    Try changing

    " and ds.statusCode in :paymentStatuses"
    

    into

    " and (:paymentStatuses is null or ds.statusCode in :paymentStatuses)"
    
    0 讨论(0)
提交回复
热议问题