You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Room多对多关系报错:无法找到子实体列

解决Room多对多关联配置错误问题

你遇到的报错是因为@Relation注解的参数配置逻辑错误,正确的配置方式需要明确关联表与父/子实体的对应字段:

修正后的关联数据类代码

data class LineWithBusStops(
    @Embedded val line: LineEntity,
    @Relation(
        parentColumn = "id", // 父实体LineEntity的主键字段
        entityColumn = "id", // 子实体BusStopEntity的主键字段
        associateBy = Junction(
            value = BusStopLineEntity::class,
            parentColumn = "lineId", // 关联表中对应LineEntity的字段
            entityColumn = "busStopId" // 关联表中对应BusStopEntity的字段
        )
    )
    val busStops: List<BusStopEntity>
)

参数说明

  • parentColumn(外层):指定父实体LineEntity的主键字段,即id
  • entityColumn(外层):指定子实体BusStopEntity的主键字段,即id
  • associateBy内的Junction参数:
    • value:指定多对多关系的中间关联表实体BusStopLineEntity
    • parentColumn:中间表中与父实体LineEntity主键关联的字段,即lineId
    • entityColumn:中间表中与子实体BusStopEntity主键关联的字段,即busStopId

这样配置后,Room就能通过中间表正确关联Line和BusStop的多对多关系,解决之前的字段匹配错误。

内容的提问来源于stack exchange,提问作者NullPointerException

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 07:45:55