Android 房间:包括嵌套对象的字段作为列

Android Room: Include Nested Object's Fields as Columns

假设我有一个 Patient 实体,存储患者 ID、一个布尔值,最后是一个 Person 对象。所以我用 @ColumnInfo 注释这些字段以存储在数据库中。

现在 Person 有 2 个字符串字段:名字和姓氏。

但是,在我的 patients table 中,我想直接为名字和姓氏字段设置一列(来自 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