Android – how to query complex nested objects in the room?

I have several entities, as you can see below. The problem is how to correctly select nested lists when you have multiple nesting levels. If you look at the Google repository on GitHub, you can find very simple examples, but not complex things. The document says here, room does not allow object references. How to get resultobject (and how it should look) including all nested lists?

Resultobject should have the following information:

>List < conversation > > dialog entity has list < message >. > message has list < imagecontent >. > imagecontent has list < imagecontentitem >

dialogue

@Entity(tableName = "dialog")
data class Dialog(@PrimaryKey val id String, val title: String)

information

@Entity(tableName = "message",
        foreignKeys = [(ForeignKey(entity = Dialog::class, parentColumns ["id"], childColumns = ["dialogId"]))])
data class Message(@PrimaryKey val id: String, val dialogId: String)

ImageContent

@Entity(tableName = "image_content",
        foreignKeys = [(ForeignKey(entity = Message::class, parentColumns = ["id"], childColumns = ["messageId"]))])
data class ImageContent(@PrimaryKey val id: String, val messageId: String)

ImageContentItem

@Entity(tableName = "image_content_item",
        foreignKeys = [(ForeignKey(entity = ImageContent::class, parentColumns = ["id"], childColumns = ["imageContentId"]))])
data class ImageContentItem(val imageContentId: String,
                        @PrimaryKey(autoGenerate = true) val id: Long = 1)

DAO:

@Dao
interface DialogDao {
    @Query("SELECT * FROM dialog " +
            "INNER JOIN message ON message.dialogId = dialog.id " +
            "INNER JOIN image_content ON image_content.messageId = message.id " +
            "INNER JOIN image_content_item ON image_content_item.imageContentId = image_content.id")
    fun getDialogAllInformation(): Flowable<List<**ResultObject**>>
}

resolvent:

Rooms use entities / tables, which do not allow you to manage structured objects as you need. The only thing you can do is execute queries separately and then build resultobjects

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>