android - 带有嵌入式 FK 的 Android Room 数据建模
问题描述
我试图代表以下实体:
Warning
:具有唯一 id、标题、副标题和最多 6 个操作的警告实体(下)Action
:具有与零个或多个警告相关的唯一 id、文本、图标等的操作实体
这种关系的例子是:
- 导弹锁定警告与动作规避机动、释放箔条和部署火炬。
- 带有动作规避机动、还击和GTFO的来袭枪声警告
这是我所拥有的:
Warning
实体_
@Entity(tableName = WARNING_TABLE,
indices = [Index(unique = true, value = [WARNING_TITLE, WARNING_SUBTITILE])]
)
data class Warning(
@field:PrimaryKey @ColumnInfo(name = WARNING_ID) var id: String,
@ColumnInfo(name = WARNING_TITLE) var text1: String,
@ColumnInfo(name = WARNING_SUBTITILE) var text2: String?,
@ColumnInfo(name = WARNING_ACTIVE, defaultValue = "0") var active: Boolean = false
)
Action
实体_
@Entity(tableName = ACTION_TABLE,
indices = [
Index(unique = true, value = [ACTION_TEXT]),
Index(unique = true, value = [ACTION_ICON])
]
)
data class Action(
@field:PrimaryKey @ColumnInfo(name = ACTION_ID) var id: String,
@ColumnInfo(name = ACTION_TEXT) var text: String,
@ColumnInfo(name = ACTION_ICON) var icon: String,
@ColumnInfo(name = ACTION_ICON_SRC, defaultValue = "$DRAWABLE_SOURCE_RESOURCES") var iconSrc: DrawableSource? = DrawableSource.Resources
)
...以及将两者联系起来的表格,warning_action
表格:
@Entity(tableName = WARNING_ACTION_TABLE,
primaryKeys = [WARNING_ID, ACTION_ID],
foreignKeys = [
ForeignKey(entity = Warning::class,
parentColumns = [WARNING_ID],
childColumns = [WARNING_ID],
onUpdate = ForeignKey.NO_ACTION,
onDelete = ForeignKey.CASCADE),
ForeignKey(entity = Action::class,
parentColumns = [ACTION_ID],
childColumns = [ACTION_ID],
onUpdate = ForeignKey.NO_ACTION,
onDelete = ForeignKey.CASCADE)
],
indices = [Index(ACTION_ID), Index(WARNING_ID)]
)
data class WarningAction(@ColumnInfo(name = ACTION_ID) val action: String,
@ColumnInfo(name = WARNING_ID) val warning: String)
现在我想做的是能够加载所有警告及其相关操作(或单个警告及其相关操作)的列表。
这是我到目前为止所拥有的,但它不起作用:
data class WarningWithActions(@Embedded val warning: Warning,
@Relation(parentColumn = WARNING_ID,
entityColumn = ACTION_ID,
associateBy = Junction(WarningAction::class))
val actions: List<Action>){
val id get() = Warning.id
}
...在 DAO 中:
@Query("SELECT * FROM '$WARNING_TABLE' WHERE $WARNING_ID = :id")
fun load (id:String): Maybe<WarningWithActions>
我也试过:
data class WarningWithActions(@Embedded val warning: Warning,
@Relation(parentColumn = WARNING_ID,
entity= Action::class,
entityColumn = ACTION_ID,
associateBy = Junction(value = WarningAction::class, parentColumn = WARNING_ID, entityColumn = ACTION_ID))
val actions: List<Action>){
val id get() = warning.id
}
我得到一个空的动作列表,我很确定我错过了一些告诉房间如何获得这些的东西,但我无法弄清楚我在这里错过了什么。
I've already used Database Inspector and verified that the actions exist, the warnings exist, and the link entries in the link table also exist.
UPDATE:
Turns out the above implementation is correct. The DAO method that was adding the action to the warning had a Single<Long>
(RX) return value:
@Transaction
@Query ("INSERT OR REPLACE INTO '$WARNING_ACTION_TABLE' ($WARNING_ID, $ACTION_ID) VALUES (:wid, :aid)")
fun addAction (eid:String, aid:String):Single<Long>
I guess at some point while writing the unit test that tested this, I was distracted and forgot to subscribe()
解决方案
推荐阅读
- python - 无法使用列表元素作为新变量的名称来创建新实例
- python - 完成训练后进行预测
- java - 在java程序上获取python数组
- c++ - 如何使用名称调用结构实例的属性
- excel - Excel:当同一个工作表中有两个表格(垂直)时,将新行添加到表格底部
- python - 音乐测验项目
- .net - “域\用户”SQL Server 2008 R2 登录失败
- javascript - 使用 fetch 将 JavaScript 数组转换为 PHP,然后发送电子邮件
- javascript - 使用 Nuxt 从组件访问 Vuex 存储状态
- android - TypeError:无法读取未定义的属性“DETECTION_MODE”-“react-native-awesome-card-io”