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

lua*_*nti 3 java spring hibernate jpql 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);
Run Code Online (Sandbox Code Playgroud)

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

Pre*_*ric 13

尝试改变

" and ds.statusCode in :paymentStatuses"
Run Code Online (Sandbox Code Playgroud)

成

" and (:paymentStatuses is null or ds.statusCode in :paymentStatuses)"
Run Code Online (Sandbox Code Playgroud)

  • 如果应该将该参数作为成员进行检查应该怎么办?我试过了但是没用:`从MyEntity a中选择一个(其中:groups为null或a.groups的:groups成员) (2认同)

Shi*_*yal 6

尝试改变

" and ds.statusCode in :paymentStatuses"
Run Code Online (Sandbox Code Playgroud)

进入

" and (COALESCE(:paymentStatuses, null) is null or ds.statusCode in :paymentStatuses)"
Run Code Online (Sandbox Code Playgroud)

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