我有实体:
@Entity
public class A {
@PrimaryKey(autoGenerate = true)
public long id;
public A() {}
}
@Entity()
public class B {
@PrimaryKey @NonNull
public String id;
public String oneCId;
public String anotherCId;
public long aId;
public B() {}
}
@Entity
public class C {
@PrimaryKey @NonNull
public String id;
public String value;
public C() {}
}
和一些pojos:
public class AWithB {
@Embedded
public A a;
@Relation(parentColumn = "id", entityColumn = "aId")
public List<BWithC> bWithC;
public AWithB() {}
}
public class BWithC {
@Embedded
public B b;
public C oneC;
public C anotherC;
public BWithC() {}
}
与dao:
@Query("SELECT * FROM a")
List<AWithB> getAllNow();
问题在于@Relation for awithb,它不能指向实体以外的其他任何东西。但是该实体不能包括其他实体。我应该如何从DB返回整个结构?
似乎您可以嵌套关系(文档页面上的Javadoc出于某种原因没有显示整个代码,因此误导了)。
它正在工作:
public class AWithB {
@Embedded
public A a;
@Relation(parentColumn = "id", entityColumn = "aId", entity = B.class)
public List<BWithC> bWithC;
public AWithB() {}
}
对于关系,您仍然可以使用@Relation注释。由于某种原因,您不能拥有简单的实例 - 您需要在这里收集。但是它正在工作:
public class BWithC {
@Embedded
public B b;
@Relation(parentColumn = "oneCId", entityColumn = "id")
public Set<C> oneC;
@Relation(parentColumn = "anotherCId", entityColumn = "id")
public Set<C> anotherC;
public BWithC() {}
}