Android房间递归关系
Android Room recursive relation
room可以有递归关系吗?
我有一个可以嵌套在 Parent/Childs 结构中的实体,类似于这样
Category1
|_________Category2
|_________Category3
| |_________Category4
|_________Category5`
我正在从从 Web 服务获得的 json 中复制此结构。
这是我当前的实体:
@Entity(tableName = "categories")
public class Category
{
@PrimaryKey
@NonNull
private String code;
private String name;
private String parentCode;
@Relation(parentColumn = "code", entityColumn = "parentCode", entity = Category.class)
private List<Category> childrens;
}
但是在编译过程中我得到了一个 Whosebug 错误:
Cause: java.lang.WhosebugError
at android.arch.persistence.room.processor.PojoProcessor.doProcess(PojoProcessor.kt:113)
at android.arch.persistence.room.processor.PojoProcessor.access$doProcess(PojoProcessor.kt:74)
at android.arch.persistence.room.processor.PojoProcessor$process.invoke(PojoProcessor.kt:105)
at android.arch.persistence.room.processor.PojoProcessor$process.invoke(PojoProcessor.kt:74)
at android.arch.persistence.room.processor.cache.Cache$Bucket.get(Cache.kt:46)
我知道我可以从实体中删除子项并迭代 json 以保存没有子项的每个类别,然后在单独的查询中通过父代码获取子项,但我只想知道是否有可能像代码中那样有一个递归关系
简短回答:不,你不能有递归关系。
长答案:
阅读有关 Relation in Room https://developer.android.com/reference/android/arch/persistence/room/Relation 的文档,我发现您不能在注释为 @Entity
的 class 中使用 @Relation 注释
Note that @Relation annotation can be used only in Pojo classes, an Entity class cannot have relations. This is a design decision to avoid common pitfalls in Entity setups. You can read more about it in the main Room documentation. When loading data, you can simply work around this limitation by creating Pojo classes that extend the Entity.
room可以有递归关系吗? 我有一个可以嵌套在 Parent/Childs 结构中的实体,类似于这样
Category1
|_________Category2
|_________Category3
| |_________Category4
|_________Category5`
我正在从从 Web 服务获得的 json 中复制此结构。
这是我当前的实体:
@Entity(tableName = "categories")
public class Category
{
@PrimaryKey
@NonNull
private String code;
private String name;
private String parentCode;
@Relation(parentColumn = "code", entityColumn = "parentCode", entity = Category.class)
private List<Category> childrens;
}
但是在编译过程中我得到了一个 Whosebug 错误:
Cause: java.lang.WhosebugError at android.arch.persistence.room.processor.PojoProcessor.doProcess(PojoProcessor.kt:113) at android.arch.persistence.room.processor.PojoProcessor.access$doProcess(PojoProcessor.kt:74) at android.arch.persistence.room.processor.PojoProcessor$process.invoke(PojoProcessor.kt:105) at android.arch.persistence.room.processor.PojoProcessor$process.invoke(PojoProcessor.kt:74) at android.arch.persistence.room.processor.cache.Cache$Bucket.get(Cache.kt:46)
我知道我可以从实体中删除子项并迭代 json 以保存没有子项的每个类别,然后在单独的查询中通过父代码获取子项,但我只想知道是否有可能像代码中那样有一个递归关系
简短回答:不,你不能有递归关系。
长答案: 阅读有关 Relation in Room https://developer.android.com/reference/android/arch/persistence/room/Relation 的文档,我发现您不能在注释为 @Entity
的 class 中使用 @Relation 注释Note that @Relation annotation can be used only in Pojo classes, an Entity class cannot have relations. This is a design decision to avoid common pitfalls in Entity setups. You can read more about it in the main Room documentation. When loading data, you can simply work around this limitation by creating Pojo classes that extend the Entity.