简体   繁体   中英

Unable to create entry inside table in Room database

I've created two @Entity(tables) in room database. 1st one for task( Task.class ) & the 2nd one for its statistic( TaskStat.class ). I want to automatically create entry in the 2nd table from the first class( Task.class ). For inserting entry in 2nd table I've created a @Dao method in TaskDao.class interface. Here is my java codes:

Task.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;
    }

TaskStat.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;
    }
}

TaskDao.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. Both two tables are created inside database but no entry in second table(task_statistic). 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述 It is throwing NullPointException . Where the problem is?

Edit:

Dependencies I've used:

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"
}

First of all you should not pass DAO in the entity class constructor. That's not a good design practice, you are adding more coupling to your code by doing that.

Also by using the in constructor level and doing like that will not maintain the lifecycle of the entity properly. i would suggest you to have TaskStat class as an attribute in the Task entity and have the cascade type as persist, so that framework will automatically persist it when you persist the Task entity.

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 

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM