Android Room递归关系 [英] Android Room recursive relation

查看:322
本文介绍了Android Room递归关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

房间是否可能具有递归关系? 我有一个可以嵌套在这样的父子结构中的实体

It's possible for room to have Recursive relations? I have an Entity that can be nested in a Parent/Childs structures something like this

Category1
 |_________Category2
 |_________Category3
 |  |_________Category4
 |_________Category5`

我正在从从WebService获得的json复制此结构.

I'm copying this structure from a json that is obtained from a WebService.

这是我当前的实体:

@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;
}

但是在编译过程中,我得到一个StackOverflow错误:

But during compiling I obtain a StackOverflow error:

原因:java.lang.StackOverflowError 在android.arch.persistence.room.processor.PojoProcessor.doProcess(PojoProcessor.kt:113) 在android.arch.persistence.room.processor.PojoProcessor.access $ doProcess(PojoProcessor.kt:74) 在android.arch.persistence.room.processor.PojoProcessor $ process $ 1.invoke(PojoProcessor.kt:105) 在android.arch.persistence.room.processor.PojoProcessor $ process $ 1.invoke(PojoProcessor.kt:74) 在android.arch.persistence.room.processor.cache.Cache $ Bucket.get(Cache.kt:46)

Cause: java.lang.StackOverflowError 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$1.invoke(PojoProcessor.kt:105) at android.arch.persistence.room.processor.PojoProcessor$process$1.invoke(PojoProcessor.kt:74) at android.arch.persistence.room.processor.cache.Cache$Bucket.get(Cache.kt:46)

我知道我可以从Entity中删除子项,并迭代json以保存每个不包含子项的Category,然后在单独的查询中通过父代码获取子项,但是我只想知道是否有可能像代码中那样具有递归关系

I know that I can remove the children from the Entity and iterate the json to save every Category without childrens, and later get the children by the parent code in a separate query, but I just want to know if it is possible to have a recursive Relation like the one in the code

推荐答案

简短答案:不,您不能具有递归关系.

Short Answer: NO you can't have recursive relations.

长答案: 阅读有关Room中关系的文档 https://developer.android.com/reference/android/arch/persistence/room/Relation 我发现您无法在标注为@Entity

Long Answer: Reading the Docs about Relation in Room https://developer.android.com/reference/android/arch/persistence/room/Relation I found that you can't use @Relation annotation inside a class annotated as @Entity

请注意,@ Relation批注只能在Pojo类中使用,而Entity类不能具有关系.这是避免在实体设置中遇到常见陷阱的设计决策.您可以在Room主文档中了解有关此内容的更多信息.加载数据时,您可以通过创建扩展实体的Pojo类来解决此限制.

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.

这篇关于Android Room递归关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆