房间持久性库中的关系
Relations in Room Persistence Library
我想在 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?
在这种情况下,在后台创建了一个关系 table,其中 User 列是父项,Pet 列是子项。它们之间的 link 是由 User 的 id 完成的,它也作为 userId 出现在 Pet 中。
Room 在这里说的是:-- "give me all the pets whose userId is matching the current id from User entity" 然后它对来自用户实体的所有 ID 重复它。
我建议您参阅此 link https://medium.com/androiddevelopers/database-relations-with-room-544ab95e4542 以便更好地理解更新后的概念。
我想在 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?
在这种情况下,在后台创建了一个关系 table,其中 User 列是父项,Pet 列是子项。它们之间的 link 是由 User 的 id 完成的,它也作为 userId 出现在 Pet 中。
Room 在这里说的是:-- "give me all the pets whose userId is matching the current id from User entity" 然后它对来自用户实体的所有 ID 重复它。
我建议您参阅此 link https://medium.com/androiddevelopers/database-relations-with-room-544ab95e4542 以便更好地理解更新后的概念。