Room: Relation entities using Room,(One to many) 检索一个有条件而不是所有元素
Room: Relation entities using Room,(One to many) Retrieve one with condition rather than all elements
我的问题与 Android Room: Insert relation entities using Room 相似
但变化不大
@Entity
public class Pet {
@PrimaryKey
public int id; // Pet id
public int userId; // User id
public String name;
//Added EXTRA column
public long time;// pet since have
}
目前 UserWithPets POJO 是
// Note: No annotation required at this class definition.
public class UserWithPets {
@Embedded
public User user;
@Relation(parentColumn = "id", entityColumn = "userId", entity = Pet.class)
public List<Pet> pets;
}
查询是
@Query("SELECT * FROM User")
public List<UserWithPets> loadUsersWithPets();
但我需要这样的东西
UserWithLatestPet POJO:
public class UserWithLatestPet {
@Embedded
public User user;
@Relation(parentColumn = "id", entityColumn = "userId", entity = Pet.class)
public Pet latestPet;//Retrieve only latest (since time) pet per user
}
//Query
@Query("SELECT * FROM User")
public List<UserWithLatestPet> loadUserWithLatestPet();
仅查询来自宠物实体和用户的最新宠物的最佳方式是什么
首先,您只能将单个 Pet 定义为 Relation 的结果,因为 Room Version 2.2.0-alpha01
我认为你可以做两件事:
1.创建查询左加入需要的宠物
2.创建正确的关系List<Pet>
然后扫描最新的List。
如果你想节省一点,你可以创建一个只包含宠物列表的时间和 ID 的 POJO,这样你就只会得到你需要的最少数据,扫描列表找到最新的然后查询为了那只宠物
我的问题与 Android Room: Insert relation entities using Room 相似 但变化不大
@Entity
public class Pet {
@PrimaryKey
public int id; // Pet id
public int userId; // User id
public String name;
//Added EXTRA column
public long time;// pet since have
}
目前 UserWithPets POJO 是
// Note: No annotation required at this class definition.
public class UserWithPets {
@Embedded
public User user;
@Relation(parentColumn = "id", entityColumn = "userId", entity = Pet.class)
public List<Pet> pets;
}
查询是
@Query("SELECT * FROM User")
public List<UserWithPets> loadUsersWithPets();
但我需要这样的东西 UserWithLatestPet POJO:
public class UserWithLatestPet {
@Embedded
public User user;
@Relation(parentColumn = "id", entityColumn = "userId", entity = Pet.class)
public Pet latestPet;//Retrieve only latest (since time) pet per user
}
//Query
@Query("SELECT * FROM User")
public List<UserWithLatestPet> loadUserWithLatestPet();
仅查询来自宠物实体和用户的最新宠物的最佳方式是什么
首先,您只能将单个 Pet 定义为 Relation 的结果,因为 Room Version 2.2.0-alpha01
我认为你可以做两件事:
1.创建查询左加入需要的宠物
2.创建正确的关系List<Pet>
然后扫描最新的List。
如果你想节省一点,你可以创建一个只包含宠物列表的时间和 ID 的 POJO,这样你就只会得到你需要的最少数据,扫描列表找到最新的然后查询为了那只宠物