I'm trying to represent the following entities:
Warning: A warning entity with a unique id, title, subtitle, and up to 6 actions (below)Action: An action entity with a unique id, text, icon, etc. relevant to zero or more warnings
Examples of this relationship would be:
- Missile Lock warning with actions Evasive Maneuver, Release Chaff, and Deploy Flare.
- Incoming gunfire warning with actions Evasive Maneuver, Return Fire, and GTFO
Here's what I have:
The Warning entity
@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
)
The Action entity
@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
)
... and the table that links the two, the warning_action table:
@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)
Now what I want to do is to be able to load a list of all Warnings and their associated Actions (or a Single warning and its associated actions).
This is what I have so far, but it isn't working:
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
}
... and in the DAO:
@Query("SELECT * FROM '$WARNING_TABLE' WHERE $WARNING_ID = :id")
fun load (id:String): Maybe<WarningWithActions>
I also tried:
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 get an empty list of actions and I'm pretty sure I'm missing something that tells room how to get those, but I can't figure out what I'm missing here.
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.
from Android Room data modeling with embedded FKs
No comments:
Post a Comment