房间持久性库中的关系

我想在Android项目中使用@Relation批注。 Here我找到了关于该主题的描述:

@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的{​​{1}}还是User的{​​{1}}?在文档中说id是父POJO中的参考列。但是父POJO是哪一个?

zqstella 回答:房间持久性库中的关系

在这种情况下,将在后台创建一个关系表,其中“用户”列是父级,而“宠物”列是子级。它们之间的链接由User的ID完成,该ID在Pet中也以userId的形式出现。

Room的意思是:“给我所有userID与User实体的当前ID匹配的宠物”,然后对User实体的所有ID重复该宠物。

我将引导您访问此链接https://medium.com/androiddevelopers/database-relations-with-room-544ab95e4542,以更好地了解更新后的概念。

本文链接:https://www.f2er.com/3041778.html

大家都在问