jOOQ 中的 UPDATE-FROM 子句抛出 CTE 字段的期望
UPDATE-FROM clause in jOOQ throws an expecption for CTE field
我正在尝试将以下 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));
当我尝试更新图书时出现以下异常:
column bat.book_id does not exist
如有任何关于如何解决此问题的建议,我们将不胜感激。 :)
这没有任何效果:
batTmp.fields("book_id", "amount");
而这只会重命名 table,而不是列:
batTmp.as("bat")
改为这样写:
batTmp.as("bat", "book_id", "amount")
我正在尝试将以下 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));
当我尝试更新图书时出现以下异常:
column bat.book_id does not exist
如有任何关于如何解决此问题的建议,我们将不胜感激。 :)
这没有任何效果:
batTmp.fields("book_id", "amount");
而这只会重命名 table,而不是列:
batTmp.as("bat")
改为这样写:
batTmp.as("bat", "book_id", "amount")