问题
I am trying to convert following PostgreSQL query into 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;
VALUES inside of FROM-clause are being created from Map<Long, Integer> bookIdsAmountMap
parameter and I am trying to perform that this way:
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;
}
}
Then, I try to also create fields which can be accessed like in this example
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));
When I try to update book I get following exception:
column bat.book_id does not exist
Any advice on how to solve this issue would be greatly appreciated. :)
回答1:
This doesn't have any effect:
batTmp.fields("book_id", "amount");
Whereas this only renames the table, not the columns:
batTmp.as("bat")
Write this instead:
batTmp.as("bat", "book_id", "amount")
来源:https://stackoverflow.com/questions/62114313/update-from-clause-in-jooq-throws-an-expecption-for-cte-field