0


我在解决<forcedType>我生成的例程中用 PL/pgSQL 编写的存储函数的问题时遇到了问题。这个问题更具体地解决了我在这个问题中已经提出的问题,并且在这个问答中部分提供了答案。

这是我在 PL/pgSQL 中遇到问题的用户定义函数之一(我只显示一个,因为两个函数具有相同的 RETURN TYPEINPUT 参数):

create or replace function public.get_order_by_order_id(o_id bigint) returns json as
$BODY$
DECLARE
   finalJson json;
BEGIN
   return finalJson;
END;
&BODY&
LANGUAGE 'plpgsql';

由于我使用的是 Maven,这些是我用来执行转换的 pom.xml 强制类型(如上面链接的 Q&A中):

<forcedType>
    <userType>java.lang.String</userType>
    <converter>
        org.jooq.Converter.ofNullable(org.jooq.JSON.class, String.class, Object::toString, org.jooq.JSON::valueOf)
    </converter>
    <includeExpression>(?i:get_book_by_book_id|return_value)</includeExpression>
</forcedType>
<forcedType>
    <userType>java.lang.Long</userType>
    <converter>
        org.jooq.Converter.ofNullable(Long.class, String.class, Object::toString, java.lang.Long::valueOf)
    </converter>
    <includeExpression>(?i:get_book_by_book_id\.b_id)</includeExpression>
</forcedType>
<forcedType>
    <userType>java.lang.String</userType>
    <converter>
        org.jooq.Converter.ofNullable(org.jooq.JSON.class, String.class, Object::toString, org.jooq.JSON::valueOf)
    </converter>
    <includeExpression>(?i:get_order_by_order_id|return_value)</includeExpression>
</forcedType>
<forcedType>
    <userType>java.lang.Long</userType>
    <converter>
        org.jooq.Converter.ofNullable(Long.class, String.class, Object::toString, java.lang.Long::valueOf)
    </converter>
    <includeExpression>(?i:get_order_by_order_id\.o_id)</includeExpression>
</forcedType>

当我在Project Exporer>Maven>Update Project中右键单击我的项目并选中“强制更新..”复选框时,我收到以下两个错误:

类型不匹配:无法从 Parameter < String > 转换为 Parameter < Long >

...这就是我生成的GetBookByBookId.java类代码的样子:

// This class is generated by jOOQ.
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class GetBookByBookId extends AbstractRoutine<String> {  
    private static final long serialVersionUID = 1276724822;

    // The parameter <code>public.get_book_by_book_id.RETURN_VALUE</code>.
    public static final Parameter<String> RETURN_VALUE = Internal.createParameter("RETURN_VALUE", org.jooq.impl.SQLDataType.JSON, false, false, org.jooq.Converter.ofNullable(org.jooq.JSON.class, String.class, Object::toString, org.jooq.JSON::valueOf));

     // The parameter <code>public.get_book_by_book_id.b_id</code>.
    // Error is occuring AT NEXT LINE of code for value of "B_ID" variable !!!!!!!!!!
    public static final Parameter<Long> B_ID = Internal.createParameter("b_id", org.jooq.impl.SQLDataType.BIGINT, false, false, org.jooq.Converter.ofNullable(Long.class, String.class, Object::toString, java.lang.Long::valueOf));

    // Create a new routine call instance
    public GetBookByBookId() {
        super("get_book_by_book_id", Public.PUBLIC, org.jooq.impl.SQLDataType.JSON, org.jooq.Converter.ofNullable(org.jooq.JSON.class, String.class, Object::toString, org.jooq.JSON::valueOf));

        setReturnParameter(RETURN_VALUE);
        addInParameter(B_ID);
    }

    // Set the <code>b_id</code> parameter IN value to the routine
    public void setBId(Long value) {
        setValue(B_ID, value);
    }

    // Set the <code>b_id</code> parameter to the function to be used with a {@link org.jooq.Select} statement
    public GetBookByBookId setBId(Field<Long> field) {
        setField(B_ID, field);
        return this;
    }
}

将字符串值strParam传递给Long.valueOf( strParam )应该可以工作,但是在 jOOQ 中由于某种原因它不起作用。Java编译器不应该能够从org.jooq.Converter.ofNullable(Long.class, String.class, Object::toString, java.lang.Long::valueOf)传入的代码中推断出它<converter>......我是否缺少一些东西,究竟是什么?
非常感谢任何形式的帮助或建议。

4

1 回答 1

1

你这样做了两次:

<forcedType>
    <userType>java.lang.Long</userType>
    <converter>
        org.jooq.Converter.ofNullable(
            Long.class, String.class, Object::toString, java.lang.Long::valueOf
        )
    </converter>
    <includeExpression>...</includeExpression>
</forcedType>

看签名Converter.ofNullable()。它的:

static <T, U> Converter<T, U> ofNullable(
    Class<T> fromType,
    Class<U> toType,
    Function<? super T, ? extends U> from,
    Function<? super U, ? extends T> to
) { ... }

其中<T>是数据库/JDBC 类型,<U>是用户类型。你只是混淆了这两种类型。该参数已经是 type Long,您正在尝试将其转换为String. 因此,将 a 声明String为您的用户类型:

<forcedType>
    <userType>java.lang.String</userType>
    ...
</forcedType>
于 2020-06-29T08:07:31.900 回答