【发布时间】:2021-10-23 10:26:38
【问题描述】:
我在定义两个实体之间的关系时遇到问题。关系是一对一的。每个设备都分配到一个房间。
设备实体:
@Entity
data class Device(
@PrimaryKey(autoGenerate = false) val ipAddress: String,
val deviceName: String,
val type: Int,
val uniqueRoomId: Int
) {
@Ignore
val status: DeviceStatus = DeviceStatus.Off
}
房间实体
@Entity
data class Room(
@PrimaryKey(autoGenerate = true) val roomId: Int,
val name: String
)
设备和房间对象:
data class DeviceAndRoom(
@Embedded val room: Room,
@Relation(
parentColumn = "roomId",
entityColumn = "uniqueRoomId",
entity = Device::class
)
val device: Device
)
DeviceDao:
@Dao
interface DeviceDao {
@Transaction
@Query("SELECT * FROM Device")
fun getAllDeviceAndRooms(): LiveData<List<DeviceAndRoom>>
[...]
}
但我收到以下构建错误:
The columns returned by the query does not have the fields [roomId,name] in com.test.mytestapp.models.DeviceAndRoom even though they are annotated as non-null or primitive. Columns returned by the query: [ipAddress,deviceName,type,uniqueRoomId]
我关注了official android guidelines,但还有其他文章描述了相同的过程,例如Database relations with Room
我不知道出了什么问题。也许有人有想法。
【问题讨论】:
标签: android sql database kotlin android-room