Home > Back-end >  JOOQ: how to fetch records grouped by field?
JOOQ: how to fetch records grouped by field?

Time:10-26

I need to get map Map<String, List<Message>> using JOOQ. I write on Kotlin. I need to group messages by usernames. My code begins at:

fun getAllForUsernames(usernames: List<String>): Map<String, List<Message>> =
        dsl.selectFrom(Messages.MESSAGES)
            .where(Messages.MESSAGES.USERNAME.`in`(usernames))
            // ??? returns Map<String, List<Message>>

Please, suggest, how to finish function.

CodePudding user response:

You can use one of the out-of-the-box mapping functions, namely ResultQuery.fetchGroups(Field, Class)

dsl.selectFrom(MESSAGES)
   .where(MESSAGES.USERNAME.`in`(usernames))
   .fetchGroups(MESSAGES.USERNAME, Message::class.java)
  • Related