问题
I have a problem when accessing values in UPDATE-FROM clause in jOOQ. I want to convert following PostgreSQL query:
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 bookIdsAmountMap parameter and I am trying to perform that this way: This is what I have done in my code so far (by Lukas Eder answer suggestion) made in this question:
Row2<Long,Integer> array[] = new Row2[bookIdAmountMap.size()];
int i = 0;
for (Map.Entry<Long, Integer> pair : bookIdAmountMap.entrySet()) {
array[i] = DSL.row(pair.getKey(), pair.getValue());
i++;
}
Table<Record2<Long, Integer>> bat = DSL.values(array);
bat = bat.as("bat", "book_id", "amount");
Field<Long> bookIdField = DSL.field(DSL.name("bat", "book_id"), Long.class);
Field<Integer> amountField = DSL.field(DSL.name("bat", "amount"), Integer.class);
ctx.update(BOOK).set(BOOK.AMOUNT, amountField).from(bat) // same result as if I am using in .from(bat.as("bat", "book_id", "amount"))
.where(BOOK.BOOK_ID.eq(bookIdField));
When I execute Java code I get following exception:
operator does not exist: bigint = text
Any help/advice on solving this issue is greatly appreciated. :)
回答1:
This issue seems related to a known issue where PostgreSQL cannot guess the right data type from a jOOQ VALUES
expression. I will need to investigate this further, and will update my answer.
A workaround is to cast your bind values to the appropriate data type explicitly:
array[i] = DSL.row(
DSL.val(pair.getKey()).cast(SQLDataType.BIGINT),
DSL.val(pair.getValue()).cast(SQLDataType.INTEGER)
);
来源:https://stackoverflow.com/questions/62147745/update-from-clause-in-jooq-throws-an-expecption-for-comparing-data-types