![](/img/trans.png)
[英]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.