UPDATE-FROM clause in jOOQ throws an expecption for CTE field

我只是一个虾纸丫 提交于 2020-06-27 16:39:28

问题


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

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