【发布时间】:2022-01-19 16:41:57
【问题描述】:
我在 Spring Data 接口中定义了以下 JPA 查询
@Query("select tr.bidder.supplierId " +
"from TaskResponse tr " +
"where tr.task.id = :taskId " +
"and tr.extended = true " +
"and tr.bidder.supplierId not in (:extendedSupplierIds)")
Set<String> supplierIdsDueTaskRevocationNotification(UUID taskId,
Set<String> extendedSupplierIds);
查询在对 Postgres 数据库执行时工作正常,但在对 H2 数据库执行时失败并出现以下错误:
Syntax error in SQL statement
SELECT BIDDER1_.SUPPLIER_ID AS COL_0_0_
FROM TASK_RESPONSES TASKRESPON0_
CROSS JOIN BIDDERS BIDDER1_
WHERE TASKRESPON0_.BIDDER_ID=BIDDER1_.ID
AND TASKRESPON0_.TASK_ID=?
AND TASKRESPON0_.EXTENDED=1
AND (BIDDER1_.SUPPLIER_ID NOT IN ()[*])";
expected "NOT, EXISTS, INTERSECTS, UNIQUE";
看起来问题出在not in (:extendedSupplierIds) 谓词上,因为如果我删除这个谓词,查询在两个数据库上执行时都不会出错。
有没有办法我可以重写查询,以便它可以在两个数据库上工作?
更新
根据一位受访者的建议,我将查询更改为使用显式联接
@Query("select b.supplierId " +
"from TaskResponse tr " +
"join tr.bidder b " +
"join tr.task t " +
"where t.id = :taskId " +
"and tr.extended = true " +
"and b.supplierId not in (:extendedSupplierIds)")
Set<String> supplierIdsDueTaskRevocationNotification(UUID taskId,
Set<String> extendedSupplierIds);
但这并没有什么不同,我仍然得到同样的错误。
【问题讨论】:
-
您是否也将方言更改为 H2Dialect?
-
@Turo 是的,我做到了
-
两个数据库发出的SQL是什么?为 :extendedSupplierIds 传递了什么,并且由于它是一个集合,“tr.bidder.supplierId 不在 :extendedSupplierIds”中是否有效或没有括号有效?似乎很奇怪,错误不会显示带有一系列“?”的 SQL 语句。括号之间
标签: java jpa spring-data-jpa h2