How to fix generating proper value type for INPUT parameter in jOOQ for generated user-defined PL/pgSQL function using <forcedType> tag?

↘锁芯ラ 提交于 2020-08-20 12:20:29

问题



I'm having an issue with resolving issue with <forcedType> for stored function written in PL/pgSQL in my generated Routines. This question is more specific for solving problem I had already asked in this question and answer is partially provided in this Q&A.

This is ONE of my user-defined functions in PL/pgSQL which I'm having issue with (I'm displaying only one since both functions have same RETURN TYPE and INPUT parameter):

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';

Since I'm using Maven these is my pom.xml forced types I've used to performed conversion (like in above linked 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>

When I perform right click on my project in Project Exporer>Maven>Update Project and check "Force Update.." checkbox I get two following errors:

Type mismatch: cannot convert from Parameter < String > to Parameter < Long >

...and this is how generated GetBookByBookId.java class code I get looks like:

// 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;
    }
}

Passing String value strParam to Long.valueOf(strParam) should work, but here in jOOQ it doesn't work for some reason. Shouldn't Java compiler be able to infer it from org.jooq.Converter.ofNullable(Long.class, String.class, Object::toString, java.lang.Long::valueOf) code passed in <converter>...is there something I'm missing and what exactly?
Any kind of help or suggestion is greatly appreciated.


回答1:


You did this twice:

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

Look at the signature of Converter.ofNullable(). It's:

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
) { ... }

Where <T> is the database / JDBC type, and <U> is the user type. You're just mixing up the two types. The parameter is already of type Long, and you're trying to convert it to String. So declare a String as your user type:

<forcedType>
    <userType>java.lang.String</userType>
    ...
</forcedType>


来源:https://stackoverflow.com/questions/62601302/how-to-fix-generating-proper-value-type-for-input-parameter-in-jooq-for-generate

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