【问题标题】:JPA : Use of list in where clause and Case statement together - Is there a way to loop through?JPA:在 where 子句和 Case 语句中一起使用列表 - 有没有办法循环?
【发布时间】:2021-11-14 14:11:43
【问题描述】:

我在我们的应用程序中使用 JPA-Hibernate,我正在处理如下查询:

@Query(
value = "ReportDTO(report) FROM Report report " +
    "JOIN p.clientFile cf " +
    "JOIN cf.client cl " +
    "WHERE report.active = TRUE " +
    "AND :companyKey = CASE WHEN report.companyKey IS NOT NULL " +
        "THEN report.companyKey " +
        "ELSE cl.companyKey " +
        "END " +
    "ORDER BY report.publishedDate DESC",
countQuery = “..”
)
@NonNull
Page<TagReportDTO> findAll(@NonNull Pageable pageable, @Param("companyKey") String companyKey);

"companyKey"single 值时,它起作用了。现在,我需要更改此 findAll 方法以接受公司密钥列表。所以它应该像(更改后):

@NonNull
Page<TagReportDTO> findAll(@NonNull Pageable pageable, @Param("companyKeys") List<String> companyKeys);

现在我很难在查询中容纳 对象列表或 companyKeys(而不是 :companyKey case 语句将适用于列表中的每个项目。有什么方法可以循环吗?

【问题讨论】:

    标签: java jpa spring-data-jpa jpql


    【解决方案1】:

    您是否尝试过使用 IN 运算符?像这样:

    "AND (CASE WHEN report.companyKey IS NOT NULL " +
    "THEN report.companyKey " +
    "ELSE cl.companyKey " +
    "END) IN :companyKeys "
    

    【讨论】:

      猜你喜欢
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      • 2021-03-14
      • 1970-01-01
      相关资源
      最近更新 更多