【问题标题】:How to skip @Param in @Query if is null or empty in Spring Data JPA如果在 Spring Data JPA 中为空或为空,如何在 @Query 中跳过 @Param
【发布时间】:2018-03-29 02:48:14
【问题描述】:
@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("paymentStatuses")

【问题讨论】:

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


【解决方案1】:

尝试改变

" and ds.statusCode in :paymentStatuses"

进入

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

【讨论】:

  • 如果参数作为成员检查应该如何?我试过这个但不起作用:Select a from MyEntity a where (:groups is null or :groups member of a.groups)
  • 对我来说也一样.. 如果我使用参数名称两次。它不起作用
  • 试试这个:" and (:#{#paymentStatuses == null} or ds.statusCode in :paymentStatuses)"
  • 如果列表包含多个项目,此解决方案将失败
  • 您能否解释一下我们能为 mongo DB 做些什么来处理这个特定的案例?
【解决方案2】:

尝试改变

" and ds.statusCode in :paymentStatuses"

进入

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

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

【讨论】:

  • 谢谢我解决了这个 oracle.jdbc.OracleDatabaseException: ORA-00920: invalid relationship operator error
  • 对于列表输入这是正确的解决方案,谢谢
猜你喜欢
  • 2018-07-05
  • 2018-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-04
  • 2021-01-03
  • 2022-01-20
  • 2022-11-15
相关资源
最近更新 更多