【问题标题】:how to retrofit and room handle nested object如何改造和房间处理嵌套对象
【发布时间】:2020-02-18 04:58:20
【问题描述】:
 "provinsi": [
        {
            "createdDate": 1490089930310,
            "createdBy": "SYSTEM",
            "updatedDate": 1490089930310,
            "updatedBy": "SYSTEM",
            "id": 31,
            "provinceName": "Kepulauan Riau",
            "provinceCode": "0",
            "mCities": [
                {
                    "createdDate": 1490092020000,
                    "createdBy": "SYSTEM",
                    "updatedDate": 1490092020000,
                    "updatedBy": "SYSTEM",
                    "provinceName": "Kepulauan Riau",
                    "id": 198,
                    "cityName": "KAB.BINTAN",
                    "cityCode": "2102",
                    "centralBankCode": ""
                },
                {
                    "createdDate": 1490092020000,
                    "createdBy": "SYSTEM",
                    "updatedDate": 1490092020000,
                    "updatedBy": "SYSTEM",
                    "provinceName": "Kepulauan Riau",
                    "id": 350,
                    "cityName": "KAB.KARIMUN",
                    "cityCode": "2101",
                    "centralBankCode": "3801"
                },

}
]

我试着把它做成一个关系表,或者两个表。找到很好的文档但这很难哈哈哈但找不到这样的东西

【问题讨论】:

  • 我试着把它做成一个关系表,或者两个表。找到很好的文档,但这很难哈哈哈,但找不到这样的东西

标签: android-room


【解决方案1】:

我认为这将是一个one-to-many 关系。例如

实体

@Entity
data class Dog(
    @PrimaryKey val dogId: Long,
    val dogOwnerId: Long,
    val name: String,
    val cuteness: Int,
    val barkVolume: Int,
    val breed: String
)

@Entity
data class Owner(@PrimaryKey val ownerId: Long, val name: String)

data class OwnerWithDogs(
    val owner: Owner,
    val dogs: List<Dog>
)

data class OwnerWithDogs(
     @Embedded val owner: Owner,
     @Relation(
          parentColumn = "ownerId",
          entityColumn = "dogOwnerId"
     )
     val dogs: List<Dog>
)

@Transaction
@Query("SELECT * FROM Owner")
fun getDogsAndOwners(): List<OwnerWithDogs>

你可以在here看到一篇好文章。

我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2019-11-18
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多