JPA:在 where 子句和 Case 语句中一起使用列表 - 有没有办法循环?
JPA : Use of list in where clause and Case statement together - Is there a way to loop through?
我在我们的应用程序中使用 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" 是一个 单一 值时,它起作用了。现在,我需要更改此 findAll 方法以接受 公司密钥列表 。所以它应该喜欢(更改后):
@NonNull
Page<TagReportDTO> findAll(@NonNull Pageable pageable, @Param("companyKeys") List<String> companyKeys);
现在我很难容纳 对象列表或 companyKeys(而不是 :companyKey) 在查询中,case 语句将适用于列表中的每个项目。有什么方法可以循环吗?
您尝试过使用 IN 运算符吗?像这样:
"AND (CASE WHEN report.companyKey IS NOT NULL " +
"THEN report.companyKey " +
"ELSE cl.companyKey " +
"END) IN :companyKeys "
我在我们的应用程序中使用 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" 是一个 单一 值时,它起作用了。现在,我需要更改此 findAll 方法以接受 公司密钥列表 。所以它应该喜欢(更改后):
@NonNull
Page<TagReportDTO> findAll(@NonNull Pageable pageable, @Param("companyKeys") List<String> companyKeys);
现在我很难容纳 对象列表或 companyKeys(而不是 :companyKey) 在查询中,case 语句将适用于列表中的每个项目。有什么方法可以循环吗?
您尝试过使用 IN 运算符吗?像这样:
"AND (CASE WHEN report.companyKey IS NOT NULL " +
"THEN report.companyKey " +
"ELSE cl.companyKey " +
"END) IN :companyKeys "