我正在使用Android中的Room作为数据库,也正在使用@Relation(基于此:https://developer.android.com/reference/android/arch/persistence/room/Relation.html) 从与ForeinKey一对多相关的关系实体中获取数据。 我想要得到的是使用@Relation从RoomArea实体中获得带有roomAreaNames的列表。该代码没有编译错误,问题在于我只是从@Relation回来的是一个大小为1的列表(仅是表中的第一个对象),而不是完整列表。
表格:
@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
)
Dao查询:
@Transaction
@Query("SELECT * FROM buildings_table WHERE contract_id = :contractId")
fun getItemsAuditBuilding(contractId: Long): LiveData<List<ItemAuditBuilding>>
这里是@Relation(仅给我一个列表大小1),我需要与buildingId相关的所有roomAreas
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>
)
谢谢。
答案 0 :(得分:0)
您正在尝试获取字符串列表。我认为您实际上打算这样做
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>
)
答案 1 :(得分:0)
我希望这对尝试使用Room关系的人有所帮助。我已根据2019年Android Dev Summit提出了我的问题的答案。以下是Florina谈到房间的部分的链接:[https://www.youtube.com/watch?v=_aJsh6P00c0]。根据视频,我进行了以下更改:
我添加了一个Junction表:
@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类
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 是一项新功能。享受吧!