【发布时间】:2020-12-17 18:50:51
【问题描述】:
我有以下 3 个实体:
@Entity(tableName = "PROPERTY",
indices = [
androidx.room.Index(value = ["id"], unique = true)
])
data class Property (
@Expose @PrimaryKey override val id: UUID = UUID.randomUUID(),
@SerializedName("type") @ColumnInfo(name = "type") val type: ItemPropertyType,
@SerializedName("code") @ColumnInfo(name = "code") val code: String,
@SerializedName("default_description") @ColumnInfo(name = "default_description") val defaultDescription: String?,
@SerializedName("description_id") @ColumnInfo(name = "description_id") val descriptionId: UUID?,
@SerializedName("default_property") @ColumnInfo(name = "default_property") val defaultProperty: Boolean,
@SerializedName("entity") @ColumnInfo(name = "entity") val entity: String = "ITEM"
)
@Entity(tableName = "PROPERTY_SET_DETAIL",
indices = [
Index(value = ["id"], unique = true),
Index(value = ["property_id"], unique = false)
],
foreignKeys = [
ForeignKey(entity = PropertySet::class, parentColumns = ["id"], childColumns = ["property_set_id"]),
ForeignKey(entity = Property::class, parentColumns = ["id"], childColumns = ["property_id"])])
data class PropertySetDetail (
@Expose @PrimaryKey override val id: UUID = UUID.randomUUID(),
@SerializedName("property_id") @ColumnInfo(name = "property_id") val propertyId: UUID,
@SerializedName("value") @ColumnInfo(name = "value") var value: String?,
@SerializedName("rank") @ColumnInfo(name = "rank") val rank: Int,
@SerializedName("property_set_id") @ColumnInfo(name = "property_set_id") val propertySetId: UUID
)
@Entity(tableName = "PROPERTY_SET",
indices = [Index(value = ["id"], unique = true), Index(value = ["properties_charset"], unique = false), Index(value = ["hashkey"], unique = false)])
data class PropertySet (
@Expose @PrimaryKey override val id: UUID = UUID.randomUUID(),
@SerializedName("item_id") @ColumnInfo(name = "item_id") val itemId: UUID,
@SerializedName("properties_charset") @ColumnInfo(name = "properties_charset") var propertiesCharset: String?,
@SerializedName("hashkey") @ColumnInfo(name = "hashkey") var hashkey: String?,
)
以及上面声明的 3 个实体之间的嵌套关系所需的以下数据类:
data class PropertySetAndDetails(
@Embedded
val set: PropertySet,
@Relation(
entity = PropertySetDetail::class,
parentColumn = "id",
entityColumn = "property_set_id"
) val details: List<PropertyDetailAndProperty>,
)
data class PropertyDetailAndProperty(
@Embedded
val property: Property,
@Relation(
parentColumn = "id",
entityColumn = "property_id"
) val detail: PropertySetDetail
)
以及如下DAO查询函数:
@Transaction
@Query("select * from property_set where item_id = :itemId")
suspend fun getSetsForItem(itemId: UUID): List<PropertySetAndDetails>
没有办法让它工作。我收到以下构建错误:
错误:查询有问题:[SQLITE_ERROR] SQL 错误或 缺少数据库(没有这样的列:类型)
我还为
声明了以下转换函数项目属性类型:
@TypeConverter
fun toItemPropertyType(v: Byte?): ItemPropertyType {
return when (v) {
null -> ItemPropertyType.Unknown
else -> ItemPropertyType.getByValue(v)
}
}
@TypeConverter
fun fromItemPropertyType(t: ItemPropertyType?): Byte {
return when (t) {
null -> ItemPropertyType.Unknown.value
else -> t.value
}
}
有人能解释一下为什么这种嵌套关系不起作用吗?提前致谢。
【问题讨论】:
标签: android kotlin relationship android-room