Android Room一对一实体关系

4

如果你想在 Room 中建立两个实体之间的关系,你可以通过一个第三方POJO来定义它们之间的关系,例如:

@Entity
class Foo {
  //fields here including _id,...
}

@Entity // forigne keys restriction here ...
class Baz {
   long foo_id
}

//The linking POJO
class BazWithFoo extends Baz { //may replaced with @Embeded Baz field 

   @Relation(entity = Foo.class,entityColumn = "_id",parentColumn = "foo_id")
   List<Foo> foo; // <-- want this to be Foo not List<Foo>
}

有没有一种好的方法可以使POJO只暴露一个字段,而不是一个List?是否有任何解决方法?

1个回答

0

你应该看到this

在你的POJO中,可以管理两个@Embedded


谢谢,那个答案的编辑部分很有帮助,我仍在寻找至少一个继承或封装的解决方法来处理列表字段作为一个对象而不是集合,直到有一个干净的解决方案。 - Hassan
如果您更新外键项,它是否会更新父查询? - TheRealChx101

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接