如何在@Query中跳过@Param,如果在Spring Data JPA中为null或为空

时间:2017-10-17 12:01:45

标签: java spring hibernate spring-data-jpa jpql

@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);

我有如上所述的查询。如果为null或为空,则可以在查询方法中跳过搜索param例如@Param("paymentStatuses")

2 个答案:

答案 0 :(得分:8)

尝试更改

" and ds.statusCode in :paymentStatuses"

进入

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

答案 1 :(得分:1)

尝试更改

" and ds.statusCode in :paymentStatuses"

进入

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

此解决方案适用于空列表,空列表以及项目为1或更多的列表。