【问题标题】:"no suitable method found" while trying to implement a custom binding尝试实现自定义绑定时“找不到合适的方法”
【发布时间】:2020-10-27 19:28:34
【问题描述】:

我在尝试为 Postgres ltrees 创建自定义绑定时收到以下消息:

[...]/jooq/routines/JSubpath1.java:37: error: no suitable method found for createParameter(String,DataType<Integer>,boolean,boolean,LtreeBinding)
    public static final Parameter<String> _2 = Internal.createParameter("_2", org.jooq.impl.SQLDataType.INTEGER, false, true, new LtreeBinding());
                                                       ^
    method Internal.<T#1>createParameter(String,DataType<T#1>,boolean,boolean) is not applicable
      (cannot infer type-variable(s) T#1
        (actual and formal argument lists differ in length))
    method Internal.<T#2,U#1>createParameter(String,DataType<T#2>,boolean,boolean,Converter<T#2,U#1>) is not applicable
      (cannot infer type-variable(s) T#2,U#1
        (argument mismatch; LtreeBinding cannot be converted to Converter<T#2,U#1>))
    method Internal.<T#3,U#2>createParameter(String,DataType<T#3>,boolean,boolean,Binding<T#3,U#2>) is not applicable
      (inference variable T#3 has incompatible equality constraints Object,Integer)
    method Internal.<T#4,X,U#3>createParameter(String,DataType<T#4>,boolean,boolean,Converter<X,U#3>,Binding<T#4,X>) is not applicable
      (cannot infer type-variable(s) T#4,X,U#3
        (actual and formal argument lists differ in length))

这是绑定:

class LtreeBinding : Binding<Any, String> {

    override fun converter(): Converter<Any, String> {
        return object : Converter<Any, String> {
            override fun from(dbAny: Any?): String? {
                return dbAny?.toString()
            }

            override fun to(userAny: String?): Any? {
                return userAny as Any
            }

            override fun fromType(): Class<Any> {
                return Any::class.java
            }

            override fun toType(): Class<String> {
                return String::class.java
            }
        }
    }

    override fun sql(ctx: BindingSQLContext<String>) {
        ctx.render()?.let {
            if (it.paramType() == ParamType.INLINED) {
                it.visit(
                    DSL.inline(ctx.convert(converter()).value())
                )
            } else {
                it.sql("?")
            }
        }
    }

    override fun register(ctx: BindingRegisterContext<String>) {
        ctx.statement().registerOutParameter(ctx.index(), Types.VARCHAR)
    }

    override fun set(ctx: BindingSetStatementContext<String>) {
        ctx.statement().setString(
            ctx.index(),
            ctx.convert(converter()).value()?.toString()
        )
    }

    override fun set(ctx: BindingSetSQLOutputContext<String>) {
        throw SQLFeatureNotSupportedException()
    }

    override fun get(ctx: BindingGetResultSetContext<String>) {
        ctx.convert(converter()).value(ctx.resultSet().getString(ctx.index()))
    }

    override fun get(ctx: BindingGetStatementContext<String>) {
        ctx.convert(converter()).value(ctx.statement().getString(ctx.index()))
    }

    override fun get(ctx: BindingGetSQLInputContext<String>) {
        throw SQLFeatureNotSupportedException()
    }
}

生成器配置是在 Gradle“构建”文件中的 XML 中完成的。生成的代码位于src/main/java/generated 中,而其余代码(包括绑定)位于src/main/kotlin

可能出了什么问题?错误日志并没有真正给我任何线索。

【问题讨论】:

  • 您的代码生成器和类路径上的运行时库的版本是否匹配?
  • 是的。所有这些都设置为 3.13.5。 @LukasEder

标签: jooq


【解决方案1】:

生成的代码如下:

Internal.createParameter("_2", SQLDataType.INTEGER, false, true, new LtreeBinding());

这意味着您的参数的数据类型是INTEGER,而不是OTHER。所以你的绑定不能是Binding&lt;Any, String&gt;类型。可能,您的绑定只是应用于存储函数的错误参数。

如果您控制该函数,请确保它使用命名参数,以便您可以通过名称将绑定附加到正确的参数。否则,您可以尝试使用 &lt;includeTypes/&gt; as documented here 按类型将其附加到正确的参数。

【讨论】:

  • includedTypes 中将.* 替换为ltree 有帮助。谢谢!
猜你喜欢
  • 2015-08-16
  • 1970-01-01
  • 1970-01-01
  • 2011-07-11
  • 1970-01-01
  • 2022-01-27
  • 2021-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多