6
Android Room:使用Room插入关系实体
我已使用Relation在Room中添加了一对多关系。我参考了这篇文章,为Room中的关系编写了以下代码。 该帖子介绍了如何从数据库中读取值,但是将实体存储到数据库中导致结果userId为空,这意味着两个表之间没有关系。 我不知道什么是理想的途径,insert一个User并List of Pet到数据库中,同时具有userId价值。 1)用户实体: @Entity public class User { @PrimaryKey public int id; // User id } 2)宠物实体: @Entity public class Pet { @PrimaryKey public int id; // Pet id public int userId; // User id public String name; } 3)UserWithPets POJO: // Note: No annotation required at …