[英]Merging two entities when querying in Room Database android
假设我们有用户和订单实体
@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();
我想要这个 output:
{"user":{"id":"","name":"",
"order":{"id":"","userId":"","meal":""}}}
您可以使用关系注释来解决这个问题。 注意,官方文档说[ 官方参考]:
使用 Relation 注释的字段的类型必须是 List 或 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.