简体   繁体   English

jOOQ 中的 UPDATE-FROM 子句引发对 CTE 字段的期望

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

I am trying to convert following PostgreSQL query into jOOQ:我正在尝试将以下 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;

VALUES inside of FROM-clause are being created from Map<Long, Integer> bookIdsAmountMap parameter and I am trying to perform that this way: 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;
    } 
}

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 bat.book_id 列不存在

Any advice on how to solve this issue would be greatly appreciated.任何有关如何解决此问题的建议将不胜感激。 :) :)

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM