UPDATE-FROM clause in jOOQ throws an expecption for comparing data types

蓝咒 提交于 2020-06-17 02:14:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!