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

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

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

我有如上查询。是否可以在查询方法中跳过搜索参数,例如 @Param("paymentStatuses") 如果为 null 或为空?

尝试更改

" and ds.statusCode in :paymentStatuses"

进入

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

尝试改变

" and ds.statusCode in :paymentStatuses"

进入

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

此解决方案适用于空列表、空列表以及包含 1 个或多个项目的列表。