我如何代表Android Room与多对多的关系? [英] How can I represent a many to many relation with Android Room?

查看:412
本文介绍了我如何代表Android Room与多对多的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何代表与Room的多对多关系? 例如我有客人"和预订".预订中可以有许多来宾,而一个来宾可以是许多预订中的一部分.

How can I represent a many to many relation with Room? e.g. I have "Guest" and "Reservation". Reservation can have many Guest and a Guest can be part of many Reservations.

这是我的实体定义:

@Entity data class Reservation(
    @PrimaryKey val id: Long,
    val table: String,
    val guests: List<Guest>
)

@Entity data class Guest(
    @PrimaryKey val id: Long,
    val name: String,
    val email: String
)

在查看文档时,我遇到了 @Relation .我发现这确实令人困惑.

While looking into docs I came across @Relation. I found it really confusing though.

据此,我想创建一个POJO并在其中添加关系.因此,以我的示例为例,我做了以下

According to this I would want to create a POJO and add the relationships there. So, with my example I did the following

data class ReservationForGuest(
    @Embedded val reservation: Reservation,
    @Relation(
        parentColumn = "reservation.id", 
        entityColumn = "id", 
        entity = Guest::class
    ) val guestList: List<Guest>
)

以上,我得到编译器错误:

With above I get the compiler error:

无法弄清楚如何从光标读取此字段.

Cannot figure out how to read this field from a cursor.

我找不到@Relation的有效样本.

推荐答案

我遇到了类似的问题.这是我的解决方法.

I had a similar issue. Here is my solution.

您可以使用一个额外的实体(ReservationGuest),该实体保持GuestReservation之间的关系.

You can use an extra entity (ReservationGuest) which keeps the relation between Guest and Reservation.

@Entity data class Guest(
    @PrimaryKey val id: Long,
    val name: String,
    val email: String
)

@Entity data class Reservation(
    @PrimaryKey val id: Long,
    val table: String
)

@Entity data class ReservationGuest(
    @PrimaryKey(autoGenerate = true) val id: Long,
    val reservationId: Long,
    val guestId: Long
)

您可以通过他们的guestId s列表获得预订. (不是来宾对象)

You can get reservations with their list of guestIds. (Not the guest objects)

data class ReservationWithGuests(
    @Embedded val reservation:Reservation,
    @Relation(
        parentColumn = "id",
        entityColumn = "reservationId",
        entity = ReservationGuest::class,
        projection = "guestId"
    ) val guestIdList: List<Long>
)

您还可以通过其reservationId列表来邀请客人. (不是保留对象)

You can also get guests with their list of reservationIds. (Not the reservation objects)

data class GuestWithReservations(
    @Embedded val guest:Guest,
    @Relation(
        parentColumn = "id",
        entityColumn = "guestId",
        entity = ReservationGuest::class,
        projection = "reservationId"
   ) val reservationIdList: List<Long>
)

由于可以获得guestIdreservationId,因此可以使用这些查询ReservationGuest实体.

Since you can get the guestIds and reservationIds, you can query Reservation and Guest entities with those.

如果我找到一种简单的方法来获取预订和来宾对象列表而不是其ID,我将更新答案.

I'll update my answer if I find an easy way to fetch Reservation and Guest object list instead of their ids.

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆