1

I am trying to use JDBI with PostgreSQL database. I am using Kotlin. Even after trying for several hours, I couldn't get to a point where I can use basic CRUD operations.

 val users = jdbi.withHandleUnchecked {
     it
        .createQuery("select id, name from users")
        .mapToBean(User::class.java)
        .list()
 }

Here is the code I am using. (There is no issue with the connection. I have verified that by executing queries using JDBC)

Here is the model class,

data class User(val id: Int, val name: String)

Here is the exception I am getting,

Exception in thread "main" java.lang.NoSuchMethodException: no such constructor: User.<init>()void/newInvokeSpecial
    at java.base/java.lang.invoke.MemberName.makeAccessException(MemberName.java:961)
    at java.base/java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:1101)
    at java.base/java.lang.invoke.MethodHandles$Lookup.resolveOrFail(MethodHandles.java:2030)
    at java.base/java.lang.invoke.MethodHandles$Lookup.findConstructor(MethodHandles.java:1264)
    at org.jdbi.v3.core.mapper.reflect.internal.BeanPropertiesFactory$BeanPojoProperties$PropertiesHolder.<init>(BeanPropertiesFactory.java:202)
    at org.jdbi.v3.core.config.JdbiCaches.lambda$declare$0(JdbiCaches.java:49)
    at org.jdbi.v3.core.config.JdbiCaches$1.lambda$get$1(JdbiCaches.java:63)
    at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1705)
    at org.jdbi.v3.core.config.JdbiCaches$1.get(JdbiCaches.java:63)
    at org.jdbi.v3.core.mapper.reflect.internal.BeanPropertiesFactory$BeanPojoProperties.getProperties(BeanPropertiesFactory.java:81)
    at org.jdbi.v3.core.mapper.reflect.internal.PojoMapper.specialize0(PojoMapper.java:99)
    at org.jdbi.v3.core.mapper.reflect.internal.PojoMapper.specialize(PojoMapper.java:80)
    at org.jdbi.v3.core.result.ResultSetResultIterator.<init>(ResultSetResultIterator.java:38)
    at org.jdbi.v3.core.result.ResultIterable.lambda$of$0(ResultIterable.java:54)
    at org.jdbi.v3.core.result.ResultIterable.stream(ResultIterable.java:228)
    at org.jdbi.v3.core.result.ResultIterable.collect(ResultIterable.java:284)
    at org.jdbi.v3.core.result.ResultIterable.list(ResultIterable.java:273)
    at MainKt$main$$inlined$withHandleUnchecked$1.withHandle(Jdbi858Extensions.kt:185)
    at org.jdbi.v3.core.Jdbi.withHandle(Jdbi.java:342)
    at MainKt.main(Main.kt:26)
    at MainKt.main(Main.kt)
Caused by: java.lang.NoSuchMethodError: User: method 'void <init>()' not found
    at java.base/java.lang.invoke.MethodHandleNatives.resolve(Native Method)
    at java.base/java.lang.invoke.MemberName$Factory.resolve(MemberName.java:1070)
    at java.base/java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:1098)
    ... 19 more

Process finished with exit code 1

Seemingly it has something to do with the User class. So, I tried tinkering with it.

Where is what I tried, (changed to var from val)

data class User(var id: Int, var name: String)

The exception being thrown is the same as above.

Here is how the dependency section of my gradle.build looks like,

dependencies {
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.31'
    implementation 'org.postgresql:postgresql:42.3.0'
    implementation 'org.jdbi:jdbi3-kotlin-sqlobject:3.23.0'
    implementation 'com.zaxxer:HikariCP:5.0.0'
    implementation 'org.slf4j:slf4j-jdk14:1.7.32'
}

Any assistance would be really helpful.

nilTheDev
  • 380
  • 3
  • 14
  • 1
    Did you configure the kotlin plugin by calling `jdbi.installPlugin(KotlinPlugin());` or `Jdbi.installPlugins()` as described in the documentation? – Augusto Oct 22 '21 at 21:12
  • 1
    @Augusto No, I wasn't doing that. Actually, I was trying to run the first example in the documentation. Now I got what I was missing. Thank you. It works! – nilTheDev Oct 23 '21 at 03:46
  • Good to hear that! Enjoy Kotlin, it's a great language! – Augusto Oct 23 '21 at 20:28

0 Answers0