Room 2.5一对一多对一嵌套关系

问题描述

房间

我有这样的关系 拼图->有一个->对话->有很多->对话行

我遵循了 https://developer.android.com/training/data-storage/room/relationships#kotlin 但仍然无法正常工作

这些似乎与错误有关:

错误:在com.example.puzzleherexamenandroid.data.room.databaseModels.DialogueWithLinesDatabase中找不到子实体列puzzleDialogueId

尝试了以下构造函数,但它们不匹配: PuzzleWithDialogueDatabase(com.example.puzzleherexamenandroid.data.room.databaseModels.PuzzleDatabase,com.example.puzzleherexamenandroid.data.room.databaseModels.DialogueWithLinesDatabase)-> [param:puzzle->匹配字段:puzzle,param:dialogueWithLines->匹配字段:unmatched] C:\ Users \ Jasper \ StudioProjects \ PuzzleHerexamenAndroid \ app \ build \ tmp \ kapt3 \ stubs \ debug \ com \ example \ puzzleherexamenandroid \ data \ room \ databaseModels \ PuzzleWithDialogueDatabase.java:9:错误:找不到以下设置器字段。

PuzzleDatabaseDao.java:12:错误:参数的类型必须是带有@Entity或其集合/数组注释的类。 java.util.List 拼图);

这些是我的实体

@Entity(tableName = "puzzle_table")
@Parcelize
data class PuzzleDatabase (
     @PrimaryKey
     val puzzleId: Int,val title: String,val prompt: String,val answer: String

): Parcelable 
 @Entity(tableName = "dialogue_table")
@Parcelize
data class DialogueDatabase (
    @PrimaryKey
    val dialogueId: Int,val char1avatar: String,val char2avatar: String,val puzzleDialogueId : Int
): Parcelable
@Entity(tableName = "dialogueLine_table")
@Parcelize
data class DialogueLineDatabase (
    @PrimaryKey
    val dialogueLineId: Int,val line: String,val speaking: Int,val dialogueForeignkeyId: Int
    ): Parcelable

这些是关系的类

data class PuzzleWithDialogueDatabase(
    @Embedded val puzzle : PuzzleDatabase,@Relation(
        entity = DialogueWithLinesDatabase::class,parentColumn = "puzzleId",entityColumn = "puzzleDialogueId"
    )
    val dialogueWithLines: DialogueWithLinesDatabase
)
data class DialogueWithLinesDatabase(
    @Embedded val dialogue:DialogueDatabase,@Relation(
        parentColumn = "dialogueId",entityColumn = "dialogueForeignkeyId"
    )
    val dialogueLines: List<DialogueLineDatabase>
)

这是我的事

@Dao
interface PuzzleDatabaseDao {
   @Transaction
   @Insert(onConflict = OnConflictStrategy.IGNORE)
   fun insertAll(puzzle: List<PuzzleWithDialogueDatabase>)

   @Transaction
   @Query("SELECT * from puzzle_table ORDER BY puzzleId DESC")
   fun getAllPuzzles(): LiveData<List<PuzzleWithDialogueDatabase>>
}

解决方法

我想您应该在DialogueWithLinesDatabase类的“版本”定义中用DialogueDatabase更改实体PuzzleWithDialogueDatabase

data class PuzzleWithDialogueDatabase(
    @Embedded val puzzle : PuzzleDatabase,@Relation(
        entity = DialogueDatabase::class,// <- changed
        parentColumn = "puzzleId",entityColumn = "puzzleDialogueId"
    )
    val dialogueWithLines: DialogueWithLinesDatabase
)
,

@Relation中的实体属性应提及数据库实体,而不是关系类。试试:

data class PuzzleWithDialogueDatabase(
    @Embedded val puzzle : PuzzleDatabase,parentColumn = "puzzleId",entityColumn = "puzzleDialogueId"
    )
    val dialogueWithLines: DialogueWithLinesDatabase
)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...