json, kotlin and kotlin-sqlobject question

168 views
Skip to first unread message

Matt Wright

unread,
Jul 3, 2019, 10:37:05 AM7/3/19
to jDBI
While giving JDBI a try for the first time with the Postgres, Kotlin, KotlinSqlObject and Jackson2 plugins enabled, I half hoped I might be able to write an SqlObject interface like so:

data class Resource(val id: String, val name: String)

interface ResourceDao {

@SqlUpdate("insert into resources (id, resource, version, created_at) values(:resource.id, :resource, 1, :now)")
@Timestamped
fun insert(@Json resource: Resource)

}

The `resource` column is a Postgres `jsonb` type. But as it turns out, and it's probably no surprise to those that know the ins and outs of JDBI, it doesn't work as I hoped it would. The `insert` method results in a "Parameter name `resource.id` not found" exception. 

So now I'm wondering if there's a way that I might write this interface, that isn't immediately obvious to me, such that an API consumer wouldn't have to explicitly pass the ID value and the resource object in two separate parameters. 

Steven Schlansker

unread,
Jul 26, 2019, 12:59:15 PM7/26/19
to jd...@googlegroups.com
Hi Matt,

Have you seen @BindBean? There's a couple examples here: http://jdbi.org/#_binding_arguments
If you want to save both the whole @Json as well as the individual properties, currently you'd have to bind it twice: once as @BindBean and once as @Json.
> --
> You received this message because you are subscribed to the Google Groups "jDBI" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jdbi+uns...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jdbi/bd3d9f06-6556-4d8d-8a6f-a128aa1fe1b5%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages