【发布时间】:2020-05-31 09:44:35
【问题描述】:
我正在尝试将以下 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 子句中的 VALUES 是从 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 列不存在
任何有关如何解决此问题的建议将不胜感激。 :)
【问题讨论】:
-
读者注意:我已恢复问题的最后编辑,因为@NikolaS 创建了a new follow up question here
标签: java postgresql sql-update jooq