I had a similar issue. Here is my solution.
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
)
You can get reservations with their list of guestId
s. (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>
)
You can also get guests with their list of reservationId
s. (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>
)
Since you can get the guestId
s and reservationId
s, you can query Reservation
and Guest
entities with those.
I'll update my answer if I find an easy way to fetch Reservation and Guest object list instead of their ids.
Similar answer
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…