假设我有一个 Patient
实体,存储患者 ID、一个 bool 值,最后是一个 Person
对象。所以我用 @ColumnInfo
注释这些字段以存储在数据库中。
现在 Person
有 2 个字符串字段:名字和姓氏。
但是,在我的 patients
表中,我想直接为名字和姓氏字段(来自 Person
)有一列,所以我想成为能够打电话firstName
(并且不必从查询中调用 Person.firstName
)。我怎样才能做到这一点?
最佳答案
可以使用Room的@Embedded注解。
你的情况如下
public class Person {
String firstName;
String lastName;
}
public class Patient {
int patientId;//just an assumption
@Embedded
Person person;
}
更多信息请查看this 注意:为简洁起见,我没有提供其他注释,例如 @ColumnInfo
关于安卓房间 : Include Nested Object's Fields as Columns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52007310/