[英]How to implement a many to many relations with Android Room Persistence Library?
[英]Relations in Room Persistence Library
我想在 Android 項目中使用@Relation
注釋。 在這里,我找到了有關該主題的描述:
@Entity
public class User {
@PrimaryKey
public int id; // User id
}
@Entity
public class Pet {
@PrimaryKey
public int id; // Pet id
public int userId; // User id
public String name;
}
public class UserWithPets {
@Embedded
public User user;
@Relation(parentColumn = "id", entityColumn = "userId", entity = Pet.class)
public List<Pet> pets;
}
我的問題是: parentColumn
參數是id
。 但它是哪個id
? 難道是id
的User
或id
的Pet
? 在文檔中它說parentColumn()
是父 POJO 中的引用列。 但哪個是父 POJO ?
在這種情況下,在后台創建了一個關系表,其中 User 列是父列,Pet 列是子列。 它們之間的鏈接由 User 的 id 完成,該 id 也作為 userId 出現在 Pet 中。
Room 在這里說的是:--“將 userId 與 User 實體中的當前 id 匹配的所有寵物給我”,然后對 User 實體中的所有 id 重復此操作。
我會向您推薦此鏈接https://medium.com/androiddevelopers/database-relations-with-room-544ab95e4542以更好地理解更新的概念。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.