小编Her*_*lva的帖子

Room Persistence 在更新时删除子项

我正在使用 Room Library 在我的 Android 应用程序中保存数据。我有 2 个主表,分别是 Task 和 Group。关系是一对多的,其中一个任务只能属于 1 个组,但一个组可以属于多个任务。

持久性工作正常,但问题是当我尝试从组更新信息时,与该组相关的每个任务都被删除。

下面是我的实体和 DAO 配置:

任务实体

@Entity(tableName = "task",
        foreignKeys = {
                @ForeignKey(
                        entity = Group.class,
                        parentColumns = "id",
                        childColumns = "groupId",
                        onDelete = CASCADE,
                        onUpdate = RESTRICT
                )},
        indices = {@Index(value = "id"), @Index(value = "groupId")}
)
public class Task {

    @PrimaryKey(autoGenerate = true)
    private int id;
    private String name;
    private int groupId;

    public Task(int id, String name, int groupId) {
        this.id = id;
        this.name = name; …
Run Code Online (Sandbox Code Playgroud)

sqlite android persistence relationship android-room

10
推荐指数
1
解决办法
2331
查看次数