Has anyone used `JDBI` with Kotlin? I have been tr...
# getting-started
n
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
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
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
Copy code
class User {
    var id: Int? = null
    var name: String? = null
}
or using default to avoid nullable types:
Copy code
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
@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
@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
516 Views