简体   繁体   English

无法在 Room 数据库中的表内创建条目

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

I've created two @Entity(tables) in room database.我在房间数据库中创建了两个@Entity(tables) 1st one for task( Task.class ) & the 2nd one for its statistic( TaskStat.class ).第一个任务( Task.class )和第二个统计数据( TaskStat.class )。 I want to automatically create entry in the 2nd table from the first class( Task.class ).我想从第一个类( Task.class )的第二个表中自动创建条目。 For inserting entry in 2nd table I've created a @Dao method in TaskDao.class interface.为了在第二个表中插入条目,我在TaskDao.class接口中创建了一个@Dao方法。 Here is my java codes:这是我的 java 代码:

Task.class任务.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任务统计.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任务道.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. 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).这两个表都是在数据库中创建的,但在第二个表(task_statistic)中没有条目。 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述 It is throwing NullPointException .它正在抛出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.首先,您不应该在实体 class 构造函数中传递 DAO。 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.此外,通过使用 in 构造函数级别并这样做不会正确维护实体的生命周期。 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.我建议您将 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