问题描述
房间
我有这样的关系 拼图->有一个->对话->有很多->对话行
我遵循了 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
)