As described in this related issue, I want to use jOOQ (3.14.4) to map a result set into a nested collection using fetchInto
.
I have the following SQL tables: USER
, USER_TO_ROLE
and ROLE
(similar to the context assumed in this answer).
In addition, I make use of PostgreSQL 13.1 with the following Jackson dependencies (with jacksonVersion="2.11.3"
:
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:$jacksonVersion")
implementation("com.fasterxml.jackson.core:jackson-databind:$jacksonVersion")
According to the docs, the following Kotlin snippet should map the users and its roles to the given Kotlin class:
val select = mutableListOf<SelectField<out Any?>>(USER.ID, USER.USERNAME)
select.add(
field(
select(
jsonArrayAgg(
jsonObject(
jsonEntry("id", ROLE.ID),
jsonEntry("name", ROLE.NAME),
)
)
)
.from(ROLE)
.join(USER_TO_ROLE).on(ROLE.ID.eq(USER_TO_ROLE.ROLE_ID))
.where(USER_TO_ROLE.user().ID.eq(USER.ID))
).`as`("roles")
)
return dsl.select(select)
.from(USER)
.where(USER.ID.eq(id))
.fetchInto(User::class.java)
Where User
and Role
are defined as:
data class User(
var id: UUID? = null,
var username: String? = null,
var roles: List<Role>? = null,
)
data class Role(
var id: UUID? = null,
var name: String? = null,
)
Unfortunately, this yields the following exception (when a User is associated with a Role):
Cannot convert from {id=<UUID>, name=<String>} (class java.util.LinkedHashMap) to class com.example.demo.Role
I assumed that adding Jackson would solve the question asked in the previously mentioned related question, but it doesn't.
Am I missing something?
A minimal sample project can be found here