0

I have a table Messages (PrimaryKey messageId) and a table ReadUpdates. Each message has many ReadUpdates that are related with

I want to query the Room Database with the below sql, and get a Map with key=Message and value=List

@Query(
        """
        Select * from messages
        inner join readUpdates on messages.messages_message_id = readUpdates.read_upd_message_id
        where messages.messages_message_id = :messageId
        and messages.messages_server_id = :serverId
    """
    )
    fun getMessageRecipients(
        serverId: Long,
        messageId: String
    ): LiveData<Map<Message, List<ReadUpdateAccount>>>

ReadUpdateAcccount is the model joined ReadUpdate and Account

data class ReadUpdateAccount(
    @Embedded
    val readUpdate: ReadUpdate,

    @Relation(entity = Account::class, parentColumn = "read_upd_connection_id", entityColumn = "accounts_account_id")
    val account: Account?
)

After debugging the query i see that instead of example

  • <Message(AAA), [ReadUpdateAccount(111), ReadUpdateAccount(222), ReadUpdateAccount(333)]>

i have

  • <Message(AAA), [ReadUpdateAccount(111)]> <Message(AAA), [ReadUpdateAccount(222)]>, <Message(AAA), [ReadUpdateAccount(333)]>

Obviously, i need the First but i get the Second Why is this happening and how can i solve this?

james04
  • 1,580
  • 2
  • 20
  • 46
  • I believe the solution made by kulukimaki [here](https://stackoverflow.com/questions/52127826/return-map-object-by-dao-method) will help you – JustSightseeing Jun 15 '22 at 13:02
  • I don't know Room, but this is how relational databases work - by joining we create a flat list with duplicated items. You would need some aggregation operation, but I'm not sure if Room supports things like that. – broot Jun 15 '22 at 13:07
  • No, i have done this before in the same way. I dont know why thats happening here – james04 Jun 15 '22 at 13:11

0 Answers0