假设我们有用户和订单实体
@Entity
public class User{
int id;
String name;
}
@Entity
public class Order{
int id;
int userId;
String meal;
}
所以当我查询时
@Query("SELECT * FROM users")
public List<User> loadUsers();
我想要这个输出:
{"user":{"id":"","name":"",
"order":{"id":"","userId":"","meal":""}}}
最佳答案
您可以使用Relation注释来解决此问题。注意,官方文档说[ official reference ]:
The type of the field annotated with Relation must be a List or Set.
@Entity
public class User{
@PrimaryKey
int id;
String name;
@Relation(parentColumn = "id", entityColumn = "orderId", entity = Order.class)
List<Order> orders;
}
@Entity
public class Order{
@PrimaryKey
int id;
int userId;
String meal;
}
关于android - 在房间数据库 android 中查询时合并两个实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58882805/