Room @Relation 仅返回 table 的第一项
Room @Relation returning only the first item of the table
我在 Android 中为我的数据库使用 Room 我也在使用 @Relation(基于此:https://developer.android.com/reference/android/arch/persistence/room/Relation.html)
从与 ForeinKey 一对多相关的关系实体中获取数据。
我想要得到的是一个使用@Relation 从 RoomArea 实体中获取 roomAreaNames 的列表。代码没有编译错误,问题是我只是从@Relation 返回的是一个大小为 1 的列表(只有 table 中的第一个对象)而不是完整列表。
表格:
@Entity(
tableName = "buildings_table",
indices = [Index("contract_id")],
foreignKeys = [
ForeignKey(
entity = Contract::class,
parentColumns = ["contract_id"],
childColumns = ["contract_id"],
onDelete = ForeignKey.CASCADE)]
)
data class Building(
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "building_id")
val buildingId: Long = 0L,
@ColumnInfo(name = "contract_id")
val contractId: Long,
@ColumnInfo(name = "building_name")
val buildingName: String)
@Entity(
tableName = "floors_table",
indices = [Index("building_id")],
foreignKeys = [
ForeignKey(
entity = Building::class,
parentColumns = ["building_id"],
childColumns = ["building_id"],
onDelete = ForeignKey.CASCADE)]
)
data class Floor(
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "floor_id")
val floor_id: Long = 0L,
@ColumnInfo(name = "building_id")
val buildingId: Long,
@ColumnInfo(name = "level")
val level: Int
)
@Entity(
tableName = "rooms_area_table",
indices = [Index("floor_id")],
foreignKeys = [
ForeignKey(
entity = Floor::class,
parentColumns = ["floor_id"],
childColumns = ["floor_id"],
onDelete = ForeignKey.CASCADE)]
)
data class RoomArea(
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "room_area_id")
val roomAreaId: Long = 0L,
@ColumnInfo(name = "floor_id")
val floorId: Long,
@ColumnInfo(name = "room_area_name")
val roomAreaName: String
)
道查询:
@Transaction
@Query("SELECT * FROM buildings_table WHERE contract_id = :contractId")
fun getItemsAuditBuilding(contractId: Long): LiveData<List<ItemAuditBuilding>>
这是@Relation(只给我一个列表大小 1)我需要与 buildingId 相关的所有房间区域
data class ItemAuditBuilding(
@Embedded val building: Building,
@Relation(
parentColumn = "building_id",
entityColumn = "room_area_id",
entity = RoomArea::class,
projection = ["room_area_name"]
)
var roomAreas: List<String>
)
谢谢。
您正在尝试获取字符串列表。我认为你实际上打算这样做
data class ItemAuditBuilding(
@Embedded val building: Building,
@Relation(
parentColumn = "building_id",
entityColumn = "room_area_id",
entity = RoomArea::class,
projection = ["room_area_name"]
)
var roomAreas: List<RoomArea>
)
我希望这对尝试使用@relation with Room 的任何人有所帮助。我的问题答案基于 Android 开发者峰会 2019。这里是 Florina 谈到房间的部分 link:[https://www.youtube.com/watch?v=_aJsh6P00c0]。根据视频,我做了以下更改:
我添加了一个 Junction table:
@Entity(
primaryKeys = ["building_id", "room_area_id"],
indices = [Index("room_area_id")]
)
data class RoomAreaWithBuilding(
@ColumnInfo(name = "building_id")
val buildingId: Long,
@ColumnInfo(name = "room_area_id")
val roomAreaId: Long
)
然后我修改了ItemAuditBuilding class
data class ItemAuditBuilding(
@Embedded val building: Building,
@Relation(
parentColumn = "building_id",
entity = RoomArea::class,
entityColumn = "room_area_id",
projection = ["room_area_name"],
associateBy = Junction(RoomAreaWithBuilding::class)
)
val roomAreas: List<String>
)
注意:要使此代码生效,您需要使用 Room 2.2,因为 associateBy 是一项新功能。享受吧!!!
我在 Android 中为我的数据库使用 Room 我也在使用 @Relation(基于此:https://developer.android.com/reference/android/arch/persistence/room/Relation.html) 从与 ForeinKey 一对多相关的关系实体中获取数据。 我想要得到的是一个使用@Relation 从 RoomArea 实体中获取 roomAreaNames 的列表。代码没有编译错误,问题是我只是从@Relation 返回的是一个大小为 1 的列表(只有 table 中的第一个对象)而不是完整列表。
表格:
@Entity(
tableName = "buildings_table",
indices = [Index("contract_id")],
foreignKeys = [
ForeignKey(
entity = Contract::class,
parentColumns = ["contract_id"],
childColumns = ["contract_id"],
onDelete = ForeignKey.CASCADE)]
)
data class Building(
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "building_id")
val buildingId: Long = 0L,
@ColumnInfo(name = "contract_id")
val contractId: Long,
@ColumnInfo(name = "building_name")
val buildingName: String)
@Entity(
tableName = "floors_table",
indices = [Index("building_id")],
foreignKeys = [
ForeignKey(
entity = Building::class,
parentColumns = ["building_id"],
childColumns = ["building_id"],
onDelete = ForeignKey.CASCADE)]
)
data class Floor(
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "floor_id")
val floor_id: Long = 0L,
@ColumnInfo(name = "building_id")
val buildingId: Long,
@ColumnInfo(name = "level")
val level: Int
)
@Entity(
tableName = "rooms_area_table",
indices = [Index("floor_id")],
foreignKeys = [
ForeignKey(
entity = Floor::class,
parentColumns = ["floor_id"],
childColumns = ["floor_id"],
onDelete = ForeignKey.CASCADE)]
)
data class RoomArea(
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "room_area_id")
val roomAreaId: Long = 0L,
@ColumnInfo(name = "floor_id")
val floorId: Long,
@ColumnInfo(name = "room_area_name")
val roomAreaName: String
)
道查询:
@Transaction
@Query("SELECT * FROM buildings_table WHERE contract_id = :contractId")
fun getItemsAuditBuilding(contractId: Long): LiveData<List<ItemAuditBuilding>>
这是@Relation(只给我一个列表大小 1)我需要与 buildingId 相关的所有房间区域
data class ItemAuditBuilding(
@Embedded val building: Building,
@Relation(
parentColumn = "building_id",
entityColumn = "room_area_id",
entity = RoomArea::class,
projection = ["room_area_name"]
)
var roomAreas: List<String>
)
谢谢。
您正在尝试获取字符串列表。我认为你实际上打算这样做
data class ItemAuditBuilding(
@Embedded val building: Building,
@Relation(
parentColumn = "building_id",
entityColumn = "room_area_id",
entity = RoomArea::class,
projection = ["room_area_name"]
)
var roomAreas: List<RoomArea>
)
我希望这对尝试使用@relation with Room 的任何人有所帮助。我的问题答案基于 Android 开发者峰会 2019。这里是 Florina 谈到房间的部分 link:[https://www.youtube.com/watch?v=_aJsh6P00c0]。根据视频,我做了以下更改:
我添加了一个 Junction table:
@Entity(
primaryKeys = ["building_id", "room_area_id"],
indices = [Index("room_area_id")]
)
data class RoomAreaWithBuilding(
@ColumnInfo(name = "building_id")
val buildingId: Long,
@ColumnInfo(name = "room_area_id")
val roomAreaId: Long
)
然后我修改了ItemAuditBuilding class
data class ItemAuditBuilding(
@Embedded val building: Building,
@Relation(
parentColumn = "building_id",
entity = RoomArea::class,
entityColumn = "room_area_id",
projection = ["room_area_name"],
associateBy = Junction(RoomAreaWithBuilding::class)
)
val roomAreas: List<String>
)
注意:要使此代码生效,您需要使用 Room 2.2,因为 associateBy 是一项新功能。享受吧!!!