https://kotlinlang.org logo
Title
n

nilTheDev

10/22/2021, 6:37 PM
Has anyone used
JDBI
with Kotlin? I have been trying for several hours but couldn't even get a basic
SELECT
statement working. Any assistance would be really helpful. https://stackoverflow.com/questions/69681588/nosuchmethodexception-in-jdbi-while-using-it-with-kotlin
s

Shawn

10/22/2021, 6:48 PM
not really a kotlin question, but the
jdbi3-kotlin-sqlobject
dependency you have in there is only a plugin—you need to have the base
jdbi-core
dependency included as well
m

Marcus Brito

10/22/2021, 9:17 PM
mapToBean
won’t really work with Kotlin data classes
at least not without some extra work from your part
you’ll have to declare your user class cumbersomely like
class User {
    var id: Int? = null
    var name: String? = null
}
or using default to avoid nullable types:
class User {
    var id: Int = 0
    var name: String = ""
}
that, or use the
jdbi3-kotlin
plugin and use
.mapTo<User>
which is probably a better option
n

nilTheDev

10/23/2021, 3:48 AM
@Marcus Brito Thank you for pointing out what I was missing. I was trying to run the first example from the documentation without realizing this is not a Kotlin first library.
m

Marcus Brito

10/25/2021, 1:24 PM
@nilTheDev you’re welcome. One of the best things about Kotlin, to me, is how friendly is it with the rest of the Java ecosystem, but you still need to understand how it works. That
mapToBean
method requires a no-args contructor, and accessor methods (get/set) for every property. Kotlin data classes, by default, won’t provide the no-arg constructor, nor set methods since they’re immutable. You can get those from regular Kotlin classes with
var
properties.
👍 1