簡體   English   中英

無法在 Room 數據庫中的表內創建條目

[英]Unable to create entry inside table in Room database

我在房間數據庫中創建了兩個@Entity(tables) 第一個任務( Task.class )和第二個統計數據( TaskStat.class )。 我想從第一個類( Task.class )的第二個表中自動創建條目。 為了在第二個表中插入條目,我在TaskDao.class接口中創建了一個@Dao方法。 這是我的 java 代碼:

任務.class

public class Task {
    @PrimaryKey
    @ColumnInfo(name = "task_ID")
    private Long taskId;

    @ColumnInfo(name = "creator_ID")
    private Long creatorId;
    private String title;
    private String description;

    @Ignore
    private TaskDao taskDao;

    public Task(String title){
        this.taskId = IdGenerator.generate();
        this.title = title;
        TaskStat taskStat = new TaskStat(getTaskId());
        taskDao.insertStat(taskStat);
    }

    public Long getTaskId() {
        return taskId;
    }

任務統計.class

@Entity(tableName = "task_statistic",
        foreignKeys = @ForeignKey(entity = Task.class, parentColumns = "task_ID",
                                    childColumns = "ownerId", onDelete = ForeignKey.CASCADE))
public class TaskStat {
    @PrimaryKey(autoGenerate = true)
    private int id;

    private long ownerId;
    private boolean completed;

    TaskStat(long ownerId) {
        this.ownerId = ownerId;
        this.completed = false;
    }

    public int getId() {
        return id;
    }
    public long getOwnerId() {
        return ownerId;
    }
    public boolean isCompleted() {
        return completed;
    }

    public void setId(int id) {
        this.id = id;
    }
    public void setCompleted(boolean completed) {
        this.completed = completed;
    }
}

任務道.class

@Dao
public interface TaskDao {
    @Insert
    void insert(Task task);

    @Insert
    void insertStat(TaskStat taskStat);

as you can see in Task.class I wanted that whenever I create an object of Task.class , it automatically creates an object of TaskStats.class and then inserts it into taskStat table. 這兩個表都是在數據庫中創建的,但在第二個表(task_statistic)中沒有條目。 在此處輸入圖像描述 在此處輸入圖像描述 在此處輸入圖像描述 它正在拋出NullPointException 問題出在哪里?

編輯:

我使用的依賴項:

dependencies {
    def lifecycle_version = "2.2.0"
    def room_version = "2.2.5"
    /*ViewModel and LiveData (New Style-in one Line)*/
    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
    annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
    /*Room Dependencies*/
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
}

首先,您不應該在實體 class 構造函數中傳遞 DAO。 這不是一個好的設計實踐,你這樣做是在給你的代碼添加更多的耦合。

此外,通過使用 in 構造函數級別並這樣做不會正確維護實體的生命周期。 我建議您將 TaskStat class 作為任務實體中的屬性,並將級聯類型設置為持久化,以便在持久化任務實體時框架將自動持久化它。

public class Task {
   @PrimaryKey
   @ColumnInfo(name = "task_ID")
   private Long taskId;

   @ColumnInfo(name = "creator_ID")
   private Long creatorId;
   private String title;
   private String description;


   @OneToOne(cascade= CascadeType.PERSIST)
   private TaskStat taskStat;

   //getters , setters 

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM