简体   繁体   English

jOOQ 中的 UPDATE-FROM 子句在比较数据类型时抛出异常

[英]UPDATE-FROM clause in jOOQ throws an exception for comparing data types

I have a problem when accessing values in UPDATE-FROM clause in jOOQ.在 jOOQ 中访问 UPDATE-FROM 子句中的值时遇到问题。 I want to convert following PostgreSQL query:我想转换以下 PostgreSQL 查询:

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: FROM 子句中的 VALUES 是从 Map bookIdsAmountMap 参数创建的,我正在尝试以这种方式执行此操作:这是我迄今为止在我的代码中所做的(由 Lukas Eder 回答建议)在这个问题中提出的:

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:当我执行 Java 代码时,我得到以下异常:

operator does not exist: bigint = text运算符不存在:bigint = text

Any help/advice on solving this issue is greatly appreciated.非常感谢有关解决此问题的任何帮助/建议。 :) :)

This issue seems related to a known issue where PostgreSQL cannot guess the right data type from a jOOQ VALUES expression.此问题似乎与 PostgreSQL 无法从 jOOQ VALUES表达式中猜测正确数据类型的已知问题有关。 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)
);

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

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