How can I represent a "many to many" relation with Room
? My column names are also the same.
e.g. I have Guest
and Reservation
. Reservation
can have many Guest
's and a Guest
can be part of many Reservations.
Here are my entity definitions:
@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.
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.
I wasn't able to find a working sample of @Relation
.