gpt4 book ai didi

Android Room 递归关系

转载 作者:行者123 更新时间:2023-12-04 17:45:24 25 4
gpt4 key购买 nike

房间可能有递归关系吗?
我有一个可以嵌套在类似这样的父/子结构中的实体

Category1
|_________Category2
|_________Category3
| |_________Category4
|_________Category5`

我从一个从 WebService 获得的 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;
}

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

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)



我知道我可以从实体中删除子项并迭代 json 以保存没有子项的每个类别,然后在单独的查询中通过父代码获取子项,但我只想知道是否可以递归与代码中的关系类似

最佳答案

简短的回答:不,你不能有递归关系。

长答案:
阅读房间里的关系文档 https://developer.android.com/reference/android/arch/persistence/room/Relation我发现您不能在注释为@Entity 的类中使用@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.

关于Android Room 递归关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52294589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com