2

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

Tomirio
  • 149
  • 3
  • 12
  • Related issue (regarding using kotlin/jackson/jOOQ/json: https://github.com/jOOQ/jOOQ/issues/11165). Your query is not projecting anything from the `USER` table. Is that intentional? – Lukas Eder Dec 28 '20 at 11:49
  • Thanks for your answer. Regarding your question: that was not intentional, and I've updated the code accordingly. I read the linked issue, but I'm not sure how I can configure jOOQ to make use of the converter. Adding the mentioned converter to my project yields the same error. Is there additional configuration involved to make that work? – Tomirio Dec 31 '20 at 11:57

0 Answers0