Room 如何使用外键获取 3 个表

Room How to fetch on 3 tables with Foreign Keys

我有一个名为 table 的用户。这个 table 有两个外键 Account 和 Shop(都是 1..1 关系)。 所以我创建了一个名为 UserAndAccount 的数据 class 和另一个名为 UserAndShop.

我现在如何获取所有这些数据(等于 UserAndAccountAndShop)?

User.kt

@Entity(
    tableName = "User"
)
data class User(

    @PrimaryKey(autoGenerate = false)
    @SerializedName("unique_id")
    var uniqueID: String,

    @SerializedName("email")
    var email: String = "",

    @SerializedName("username")
    var username: String = ""
)

Account.kt

@Entity(
    tableName = "Account"
)
data class Account(

    @PrimaryKey(autoGenerate = true)
    @SerializedName("account_id")
    var accountID: Int,

    @SerializedName("user_id")
    var userOwnerID: String = "",

    @SerializedName("token")
    var token: String = ""
)

Shop.kt

@Entity(
    tableName = "Shop"
)
data class Shop(

    @PrimaryKey(autoGenerate = true)
    @SerializedName("shop_id")
    var shopID: Int,

    @SerializedName("shop_owner_id")
    var shopOwnerID: String,

    @SerializedName("shop_name")
    var shopName: String = ""
)

用户和Account.kt

data class UserAndAccount(
    @Embedded val user: User,
    @Relation(
        parentColumn = "uniqueID",
        entityColumn = "userOwnerID"
    )
    val account: Account
)

用户和Shop.kt

data class UserAndShop(
    @Embedded val user: User,
    @Relation(
        parentColumn = "uniqueID",
        entityColumn = "shopOwnerID"
    )
    val shop: Shop
)

致所有寻求答案的人:

您可以将多个@Relation 添加到一个数据中class。 所以我创建了这个数据 class:

UserAndAccountAndShop.kt

data class UserAndAccountAndShop(
    @Embedded val user: User,
    @Relation(
        parentColumn = "uniqueID",
        entityColumn = "accountOwnerID"
    )
    val account: Account,

    @Relation(
        parentColumn = "uniqueID",
        entityColumn = "shopOwnerID"
    )
    val shop: Shop
)

就是这样!