我在 Room 数据库上设置了多对多关系,如下图所示:
我的问题是,当在查询中构造 recipeWithIngredients 时,如何让 RecipeWithIngredients 从 RecipeIngredientRef 实体获取此单元:字符串变量?
@Entity(indices = [Index(value = ["name"],unique = true)])
data class Recipe(
var name: String,
val image: String?
) {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "recipeID")
var ID: Long = 0
}
@Entity(indices = [Index(value = ["name"],unique = true)])
data class Ingredient(
val name: String,
val image: String?,
val amount: Int
) {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "ingredientID")
var ID: Long = 0
}
@Entity(primaryKeys = ["recipeID", "ingredientID"])
data class RecipeIngredientRef(
val recipeID: Long,
val ingredientID: Long,
val unit: String
)
data class RecipeWithIngredients(
@Embedded
val recipe: Recipe,
@Relation(
parentColumn = "recipeID",
entity = Ingredient::class,
entityColumn = "ingredientID",
associateBy = Junction(
value = RecipeIngredientRef::class,
parentColumn = "recipeID",
entityColumn = "ingredientID"
)
)
val ingredients: List<Ingredient>
)