首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kotlin对JDBI SqlObject的支持给出了UnsupportedOperationException

Kotlin对JDBI SqlObject的支持给出了UnsupportedOperationException
EN

Stack Overflow用户
提问于 2018-07-20 07:12:42
回答 1查看 2.2K关注 0票数 1

扩展JDBI3设置的Kotlin等价物列出了在正式的Dropwizard文档中,没有@Bind和特定于Kotlin的映射魔术,JDBI无法获得自动参数绑定,如Kotlin对SqlObject的支持所示。而不是这个..。

代码语言:javascript
复制
data class Thing(val id: Int, val name: String,
                 val nullable: String?,
                 val nullableDefaultedNull: String? = null,
                 val nullableDefaultedNotNull: String? = "not null",
                 val defaulted: String = "default value")

interface ThingDao {
    @SqlUpdate("insert into something (id, name) values (:something.id, :something.name)")
    fun insert(something: Thing)

    @SqlQuery("select id, name from something")
    fun list(): List<Thing>

    ...
}

..I总是必须这样做:

代码语言:javascript
复制
interface ThingDao {
    @SqlUpdate("insert into something (id, name) values (:id, :name)")
    fun insert(@Bind("id") id: Int?, @Bind("name") name: String)

    @SqlQuery("select id, name from something")
    fun list(): List<Thing>

    ...
}

Gradle具有以下特定于JDBI的设置:

代码语言:javascript
复制
...
compile "io.dropwizard:dropwizard-jdbi3:1.3.5"
compile "org.jdbi:jdbi3-sqlobject:3.3.0"
compile "org.jdbi:jdbi3-postgres:3.3.0"
compile "org.jdbi:jdbi3-kotlin:3.3.0"
compile "org.jdbi:jdbi3-kotlin-sqlobject:3.3.0"
....

Dropwizard应用程序具有以下运行配置:

代码语言:javascript
复制
override fun run(configuration: MyConfig, environment: Environment) {
    val factory = JdbiFactory()
    val jdbi = factory.build(environment, configuration.database, "postgresql")
    // This is said to install all available plugins and is thus redundant.
    // I have tried to include various combinations of the following in
    // some desperation. None work.
    jdbi.installPlugins()
    jdbi.installPlugin(SqlObjectPlugin())  // This...
    jdbi.installPlugin(KotlinPlugin())
    jdbi.installPlugin(KotlinSqlObjectPlugin())  // ..and this alone are said to do the job
    ...

否则,所有事情似乎都会在自定义UUID映射、Jackson Kotlin数据对象映射等方面运行得很好。

特别是使用:something.id的结果总是:

代码语言:javascript
复制
java.lang.UnsupportedOperationException: No argument factory registered for 'Thing(...'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-23 09:05:13

意见中提出的解决办法都是行之有效的。非常感谢!这个问题似乎是在一个不幸的过程中出现的:添加了正确的库,并在一个过程中修复了代码,这给人留下了不起作用的印象(我仍然不喜欢注释--狂热)。

任何可能在其中发现任何有用内容的人的摘要:

  1. 添加@BindBean使数据类确实可以作为参数工作。
  2. @Bind排除在争论之外显然有很长一段时间了,我只是没有注意到.
  3. 由于我也不正确地尝试在数据类中使用..possibly:@Bind("something") something: Thing而不是正确的(但仍然不必要的) @BindBean("something") something: Thing
  4. 考虑到这一点,删除@Bind@BindBean是有效的(至少在1001级的干净构建中是这样的)。
  5. 我删除了compile "org.jdbi:jdbi3-sqlobject:$jdbi_version",这两种方式似乎对正常工作的事物都没有任何影响(据说Dropwizard已经引用了它)。

为了记录在案,下面这些依赖关系现在似乎对我有用:

代码语言:javascript
复制
buildscript {
  ext.kotlin_version = '1.2.51'
  ext.dropwizard_version = '1.3.5'
  ext.jdbi_version = '3.3.0'
  ...
}

...

dependencies {
  compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
  compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

  compile "io.dropwizard:dropwizard-core:$dropwizard_version"
  compile "io.dropwizard:dropwizard-jdbi3:$dropwizard_version"
  compile "io.dropwizard:dropwizard-auth:$dropwizard_version"
  compile "io.dropwizard:dropwizard-views-freemarker:$dropwizard_version"
  // This is to serve static content from resources/static
  compile "io.dropwizard:dropwizard-assets:$dropwizard_version"

  compile 'com.fasterxml.jackson.module:jackson-module-kotlin:2.9.4'
  compile 'com.fasterxml.jackson.module:jackson-modules-java8:2.9.4'

  // Removed as redundant
  // compile "org.jdbi:jdbi3-sqlobject:$jdbi_version"
  compile "org.jdbi:jdbi3-postgres:$jdbi_version"
  // So, these are still required as Dropwizard doesn't know Kotlin
  compile "org.jdbi:jdbi3-kotlin:$jdbi_version"
  compile "org.jdbi:jdbi3-kotlin-sqlobject:$jdbi_version"

  // Database
  compile 'org.postgresql:postgresql:42.1.4'

  // ...

  testCompile "io.dropwizard:dropwizard-testing:$dropwizard_version"

  // ...
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51437036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档