【问题标题】:Relations in Room Persistence Library房间持久性库中的关系
【发布时间】:2020-03-19 22:40:36
【问题描述】:

我想在 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?是User 的id 还是Pet 的id?在文档中,它说parentColumn() 是父 POJO 中的参考列。但是哪个是父 POJO 呢?

【问题讨论】:

    标签: android android-room pojo


    【解决方案1】:

    在这种情况下,会在底层创建一个关系表,其中 User 列是父列,Pet 列是子列。它们之间的链接由 User 的 ID 完成,该 ID 在 Pet 中也以 userId 的形式存在。

    Room 在这里说的是:--“给我所有其 userId 与来自用户实体的当前 id 匹配的宠物”,然后它对来自用户实体的所有 id 重复它。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 2018-01-22
      • 1970-01-01
      • 1970-01-01
      • 2017-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多