根据the documentation关系只能引用Pojo类。如何定义相同类型的嵌套依赖项?
@Entity(tableName = "humans")
data class Human {
@Relation
val children:List<Human>
}
答案 0 :(得分:1)
根据this中型博客文章,您可以使用类型转换器将列表转换为字符串,反之亦然。
class HumanTypeConverters {
private var gson = Gson()
@TypeConverter
fun stringToHumanObjectList(data: String?): List<Human> {
if (data == null) {
return Collections.emptyList()
}
return gson.fromJson(data, object : TypeToken<List<Human>>() {}.type)
}
@TypeConverter
fun humanObjectListToString(humanObjects: List<Human>): String = gson.toJson(humanObjects)
}
并按如下所示添加此注释
@Database(entities = [Human::class], version = 1)
@TypeConverters(HumanTypeConverters::class)
abstract class HumanDatabase : RoomDatabase() {
...
}