Android 房间关系 returns 无效列表
Android Room Relation returns an invalid list
我有一个复杂关系的查询。
data class GameWithHouseworkResult(
@Embedded val game:GameClass,
@Relation(
entity = GameHouseworkResult::class,
parentColumn = "gameId",
entityColumn = "houseworkId" ,
associateBy = Junction(value=GameHouseworkResult::class)
)
val houseworkAndResult: List<HouseworkAndResult>
)
我有 3 个实体和另外一个 class
@Entity
class GameClass(
@PrimaryKey(autoGenerate = true)
var gameId: Int = 0,
val mainPlayer:String,
var secondPlayer:String=""
}
@Entity
data class HouseworkClass(
@PrimaryKey
val houseworkId: Int,
val name:String,
val score:Int
) {
}
@Entity(primaryKeys = ["gameId", "houseworkId"])
class GameHouseworkResult(
var gameId: Long,
val houseworkId:Long,
val userToken:String,
val isDone:Boolean?= null) {
}
data class HouseworkAndResult(
@Embedded val gameHouseworkResult:GameHouseworkResult,
@Relation(
parentColumn = "houseworkId",
entityColumn = "houseworkId"
)
val housework: HouseworkClass
)
GameHouseworkResult 是一个活页夹class,它包含以下数据
@Transaction
@Query("SELECT * FROM GameClass where gameId=:id")
suspend fun getGameWithHouseworkResult(id:Long): GameWithHouseworkResult
当我用 gameId = 26
发送请求时,我应该得到一个包含五个元素的列表 (houseworkAndResult),但我得到一个包含 10 个元素的列表,每个元素都表示 gameId = 26
。
这很可能是由于相同的 houseworkId,但我无法理解在绘制关系时哪里出错了。请帮助我!
别担心,我已经找到解决办法了。我混淆了 @relation 并使用多对多而不是一对多。正确代码
data class GameWithHouseworkResult(
@Embedded val game:GameClass,
@Relation(
entity = GameHouseworkResult::class,
parentColumn = "gameId",
entityColumn = "gameId"
)
val houseworkAndResult: List<HouseworkAndResult>
)
我有一个复杂关系的查询。
data class GameWithHouseworkResult(
@Embedded val game:GameClass,
@Relation(
entity = GameHouseworkResult::class,
parentColumn = "gameId",
entityColumn = "houseworkId" ,
associateBy = Junction(value=GameHouseworkResult::class)
)
val houseworkAndResult: List<HouseworkAndResult>
)
我有 3 个实体和另外一个 class
@Entity
class GameClass(
@PrimaryKey(autoGenerate = true)
var gameId: Int = 0,
val mainPlayer:String,
var secondPlayer:String=""
}
@Entity
data class HouseworkClass(
@PrimaryKey
val houseworkId: Int,
val name:String,
val score:Int
) {
}
@Entity(primaryKeys = ["gameId", "houseworkId"])
class GameHouseworkResult(
var gameId: Long,
val houseworkId:Long,
val userToken:String,
val isDone:Boolean?= null) {
}
data class HouseworkAndResult(
@Embedded val gameHouseworkResult:GameHouseworkResult,
@Relation(
parentColumn = "houseworkId",
entityColumn = "houseworkId"
)
val housework: HouseworkClass
)
GameHouseworkResult 是一个活页夹class,它包含以下数据
@Transaction
@Query("SELECT * FROM GameClass where gameId=:id")
suspend fun getGameWithHouseworkResult(id:Long): GameWithHouseworkResult
当我用 gameId = 26
发送请求时,我应该得到一个包含五个元素的列表 (houseworkAndResult),但我得到一个包含 10 个元素的列表,每个元素都表示 gameId = 26
。
这很可能是由于相同的 houseworkId,但我无法理解在绘制关系时哪里出错了。请帮助我!
别担心,我已经找到解决办法了。我混淆了 @relation 并使用多对多而不是一对多。正确代码
data class GameWithHouseworkResult(
@Embedded val game:GameClass,
@Relation(
entity = GameHouseworkResult::class,
parentColumn = "gameId",
entityColumn = "gameId"
)
val houseworkAndResult: List<HouseworkAndResult>
)