如何使用 <forcedType> 标记为生成的用户定义 PL/pgSQL 函数修复在 jOOQ 中为 INPUT 参数生成正确的值类型?
How to fix generating proper value type for INPUT parameter in jOOQ for generated user-defined PL/pgSQL function using <forcedType> tag?
我在解决生成例程中用 PL/pgSQL 编写的存储函数的 <forcedType>
问题时遇到了问题。这个问题更具体地解决了我在 this question and answer is partially provided in Q&A 中已经问过的问题。
这是我在 PL/pgSQL 中遇到问题的用户定义函数之一(我只显示一个,因为这两个函数 相同 RETURN TYPE 和 INPUT 参数):
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 强制类型(如上面 链接的问答 ):
<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 中右键单击我的项目并选中“Force Update..”复选框时,我得到 two 以下错误:
Type mismatch: cannot convert from Parameter < String > to
Parameter < Long >
...这就是我得到的 GetBookByBookId.java class 代码的生成方式:
// 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 编译器不应该能够从 <converter>
中传递的 org.jooq.Converter.ofNullable(Long.class, String.class, Object::toString, java.lang.Long::valueOf)
代码中推断出它吗?我是否遗漏了什么?究竟是什么?
非常感谢任何形式的帮助或建议。
你做了两次:
<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>
是用户类型。你只是混淆了这两种类型。该参数已经是 Long
类型,您正在尝试将其转换为 String
。所以声明一个 String
作为你的用户类型:
<forcedType>
<userType>java.lang.String</userType>
...
</forcedType>
我在解决生成例程中用 PL/pgSQL 编写的存储函数的 <forcedType>
问题时遇到了问题。这个问题更具体地解决了我在 this question and answer is partially provided in
这是我在 PL/pgSQL 中遇到问题的用户定义函数之一(我只显示一个,因为这两个函数 相同 RETURN TYPE 和 INPUT 参数):
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 强制类型(如上面 链接的问答 ):
<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 中右键单击我的项目并选中“Force Update..”复选框时,我得到 two 以下错误:
Type mismatch: cannot convert from Parameter < String > to Parameter < Long >
...这就是我得到的 GetBookByBookId.java class 代码的生成方式:
// 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 编译器不应该能够从 <converter>
中传递的 org.jooq.Converter.ofNullable(Long.class, String.class, Object::toString, java.lang.Long::valueOf)
代码中推断出它吗?我是否遗漏了什么?究竟是什么?
非常感谢任何形式的帮助或建议。
你做了两次:
<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>
是用户类型。你只是混淆了这两种类型。该参数已经是 Long
类型,您正在尝试将其转换为 String
。所以声明一个 String
作为你的用户类型:
<forcedType>
<userType>java.lang.String</userType>
...
</forcedType>