My Question is similar with Android Room: Insert relation entities using Room but with little changes
@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
}
Currently UserWithPets POJO is
// 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;
}
And Query is
@Query("SELECT * FROM User")
public List<UserWithPets> loadUsersWithPets();
But I need something like that 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();
What will be best way to query with only latest pet from Pet Entity along with User