我如何表示“多对多"?列名相同时与 Android Room 的关系? [英] How can I represent a "many to many" relation with Android Room when column names are same?

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

问题描述

我如何表示多对多"?与房间的关系?我的列名也一样.

How can I represent a "many to many" relation with Room? My column names are also the same.

例如我有 GuestReservation.Reservation 可以有多个 Guest,一个 Guest 可以是多个 Reservation 的一部分.

e.g. I have Guest and Reservation. Reservation can have many Guest's 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
)

在查看文档时,我遇到了 @关系.不过我发现它真的很混乱.

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 wasn't able to find a working sample of @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 列表进行预订.(不是来宾对象)

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>
)

因为你可以得到 guestIds 和 reservationIds,你可以查询 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.

类似的答案

这篇关于我如何表示“多对多"?列名相同时与 Android Room 的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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