jOOQ 中的 UPDATE-FROM 子句抛出 CTE 字段的表达式



我正在尝试将以下PostgreSQL查询转换为jOOQ:

UPDATE book
SET amount = bat.amount
FROM (
VALUES (2, 136),(5, 75)
) AS bat(book_id, amount)
WHERE book.book_id = bat.book_id;

FROM-子句中的值是从Map<Long, Integer> bookIdsAmountMap参数创建的,我正在尝试以这种方式执行此操作:

class BookUtilHelper {
@SuppressWarnings("unchecked")
static Table<Record2<Long, Integer>> batTmp(DSLContext dsl, Map<Long, Integer> bookIdAmountMapUpdated) {
Row2<Long,Integer> array[] = new Row2[bookIdAmountMapUpdated.size()];
int i = 0;
for (Map.Entry<Long, Integer> pair : bookIdAmountMapUpdated.entrySet()) {
array[i]=DSL.row(pair.getKey(), pair.getValue());
i++;
}
Table<Record2<Long, Integer>> batTmp = DSL.values(array);
batTmp.fields("book_id", "amount");         
return batTmp;
} 
}

然后,我也尝试创建可以访问的字段,如本例所示

Field<Long> bookIdField = DSL.field(DSL.name("bat", "book_id"), Long.class);
Field<Integer> amountField = DSL.field(DSL.name("bat", "amount"), Integer.class);
Table<Record2<Long, Integer>> batTmp = BookUtilHelper.batTmp(dsl, bookIdAmountMapUpdated);
// ctx variable is of type DSLContext
ctx.update(BOOK).set(BOOK.AMOUNT, amountField).from(batTmp.as("bat")) 
.where(BOOK.BOOK_ID.eq(bookIdField));

当我尝试更新书籍时,出现以下异常:

列 bat.book_id 不存在

有关如何解决此问题的任何建议将不胜感激。 :)

这没有任何效果:

batTmp.fields("book_id", "amount");

而这只会重命名表,而不是列:

batTmp.as("bat")

改为写这个:

batTmp.as("bat", "book_id", "amount")

最新更新