Android 如何在kotlin with room中插入/删除一对多关系中的所有表?

Android 如何在kotlin with room中插入/删除一对多关系中的所有表?,android,sql,android-studio,kotlin,android-room,Android,Sql,Android Studio,Kotlin,Android Room,我拥有以下实体: @Entity(tableName = "match_frames_table") data class DbFrame( @PrimaryKey(autoGenerate = false) val frameId: Int ) 在一个数据类下面,引用3个其他实体,包括一个嵌套实体,DbBreakWithPots: data class DbFrameWithScoreAndBreakWithPotsAndBallStack( @E

我拥有以下实体:

@Entity(tableName = "match_frames_table")
data class DbFrame(
    @PrimaryKey(autoGenerate = false)
    val frameId: Int
)
在一个数据类下面,引用3个其他实体,包括一个嵌套实体,
DbBreakWithPots

data class DbFrameWithScoreAndBreakWithPotsAndBallStack(
    @Embedded val frame: DbFrame,
    @Relation(
        parentColumn = "frameId",
        entityColumn = "frameId",
    )
    val frameScore: List<DbScore>,
    @Relation(
        parentColumn = "frameId",
        entityColumn = "frameId",
        entity = DbBreak::class
    )
    val frameStack: List<DbBreakWithPots>,
    @Relation(
        parentColumn = "frameId",
        entityColumn = "frameId"
    )
    val ballStack: List<DbBall>
)
在我的DAO中,我实现了查询方法,效果很好:

@Query("SELECT * FROM match_frames_table")
fun getMatchFrames(): LiveData<List<DbFrameWithScoreAndBreakWithPotsAndBallStack>>
@Query(“从匹配帧表中选择*)
fun getMatchFrames():LiveData

然而,目前我正在一个表一个表地手动插入和删除数据库中的数据,但我有嵌套关系这一事实使它变得棘手。有没有一种方法可以简单地插入
DbFrame
并通过一个sql请求将其删除?

onUpdateonDelete操作的CASCADE选项完成这项工作

更专门地定义子实体中的外键,例如:-

@Entity(
    foreignKeys = [
        ForeignKey(entity = DBIdTotal::class, // Parent Entity
            parentColumns = ["id_DBIdTotal"], // column(s) in the parent
            childColumns = ["ref_DBIdTotal"], // column(s) in this table (the child)
            onDelete = ForeignKey.CASCADE, //<<<<<<<<<< if parent is deleted, the deletion is cascaded to the respective children and they are updated
            onUpdate = ForeignKey.CASCADE //<<<<<<<<<< if parent's referenced column(s) is updated then the updated value is changed in the respective children
        )
    ]
)
data class DBIdOpPedido(
    @PrimaryKey
    val id_DBIdOpPedido: Long,
    val ref_DBIdTotal: Long,
    val op: String
)
@实体(
外键=[
ForeignKey(entity=DBIdTotal::class,//父实体
parentColumns=[“id\u DBIdTotal”],//父目录中的列
childColumns=[“ref_DBIdTotal”],//此表中的列(子列)
onDelete=ForeignKey.CASCADE//
@Entity(
    foreignKeys = [
        ForeignKey(entity = DBIdTotal::class, // Parent Entity
            parentColumns = ["id_DBIdTotal"], // column(s) in the parent
            childColumns = ["ref_DBIdTotal"], // column(s) in this table (the child)
            onDelete = ForeignKey.CASCADE, //<<<<<<<<<< if parent is deleted, the deletion is cascaded to the respective children and they are updated
            onUpdate = ForeignKey.CASCADE //<<<<<<<<<< if parent's referenced column(s) is updated then the updated value is changed in the respective children
        )
    ]
)
data class DBIdOpPedido(
    @PrimaryKey
    val id_DBIdOpPedido: Long,
    val ref_DBIdTotal: Long,
    val op: String
)