【发布时间】:2022-08-24 06:19:50
【问题描述】:
我已经按照documentation 对 Room 中的嵌套关系进行建模,但尽管如此,我仍无法正确检索数据。我已经使用数据库检查器检查了表格,并且所有内容都正确填写,问题似乎出在建模关系上。
数据模型:
房间桌:
@Entity(tableName = \"journey\")
data class JourneyEntity(
@PrimaryKey(autoGenerate = false)
val journeyId: Int,
val name: String,
val created: String,
val modified: String,
val date: String
)
@Entity(tableName = \"agenda\")
data class AgendaEntity(
@PrimaryKey(autoGenerate = false)
val agendaId: Int,
val name: String?
)
@Entity(tableName = \"stage\")
data class StageEntity(
@Embedded val excursion: ExcursionEntity,
@PrimaryKey(autoGenerate = false)
val stageId: Int,
val stageType: String
)
@Entity(primaryKeys = [\"agendaId\", \"stageId\"], tableName = \"agendaStageCrossRef\")
data class AgendaStageCrossRef(
val agendaId: Int,
val stageId: Int
)
@Entity(primaryKeys = [\"journeyId\", \"agendaId\"], tableName = \"journeyAgendaCrossRef\")
data class JourneyAgendaCrossRef(
val journeyId: Int,
val agendaId: Int
)
data class AgendasWithStages(
@Embedded val agenda: AgendaEntity,
@Relation(
parentColumn = \"agendaId\",
entityColumn = \"stageId\",
associateBy = Junction(AgendaStageCrossRef::class)
)
val stages: List<StageEntity>
)
data class JourneyWithAgendas(
@Embedded val journey: JourneyEntity,
@Relation(
entity = AgendaEntity::class,
parentColumn = \"journeyId\",
entityColumn = \"agendaId\",
)
val agendas: List<AgendasWithStages>
)
agendas in JourneyWithAgendas 执行后始终为空:
@Transaction
@Query(\"SELECT * FROM journey\")
suspend fun getJourneys(): List<JourneyWithAgendas>
标签: android kotlin android-room android-room-relation