我在 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);
但這沒有區別,我仍然遇到相同的錯誤。
uj5u.com熱心網友回復:
這是由一個錯誤引起的,可能在 org.hibernate.dialect.H2Dialect 中。
如果您傳遞一個空集作為查詢引數值,該值構成 JPQL 查詢中“not in”謂詞的右側,則該查詢在針對 H2 資料庫運行時將失敗。要解決此問題,請改用包含單個空元素的 Set。
例如,在您呼叫此查詢的地方使用
UUID taskId = UUID.randomUUID();
Set<String> supplierIds = // get this from somewhere
if (supplierIds.isEmpty()) {
supplierIds.add(null);
}
myRepository.supplierIdsDueTaskRevocationNotification(taskId, supplierIds)
uj5u.com熱心網友回復:
你可以檢查這個SQLGrammarException: could not prepare statement。看來您遇到了以下同樣的問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/386167.html
