我正在尝试将以下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;
}
}
然后,我也尝试创建可以像this示例中那样访问的字段
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")