Room SQL自动生成主键,返回0或null

时间:2019-02-07 00:37:00

标签: android mysql android-room

所以我的数据库正在与一个网站实体合作

 @Entity(tableName = "website_table") public class Website {

     @NonNull
     @PrimaryKey(autoGenerate = true)
     private Integer websiteId;

     private String title;
     private String base_URL;
     private String description;


     public Website(String title, String base_URL) {
         this.title = title;
         this.base_URL = base_URL;
     }

     public void setWebsiteId(Integer websiteId) {
         this.websiteId = websiteId;
     }

     public Integer getWebsiteId() {
         return websiteId;
     }

     public String getTitle() {
         return title;
     }

     public String getDescription() {
         return description;
     }

     public void setDescription(String description) {
         this.description = description;
     }

     public String getBase_URL() { return base_URL; }

     public void setBase_URL(String base_URL) { this.base_URL = base_URL; }
   }

@Dao
public interface WebsiteDao {

    @Insert
    void insert(Website website);

    @Update
    void update(Website website);

    @Delete
    void delete(Website website);

    @Query("DELETE FROM website_table")
    void deleteAllWebsites();

    @Query("SELECT * FROM website_table ORDER BY title DESC")
    LiveData<List<Website>> getAllWebsites();

    @Query("SELECT * FROM website_table")
    public List<WebsiteWithWebPages> loadWebsitesWithWebPages();
}

然后我尝试为网站上的网页添加关系

    public class WebsiteWithWebPages {
        @Embedded
        public Website website;

        @Relation(parentColumn = "websiteId", entityColumn = "website_Id", 

        entity = WebPage.class)
        public List<WebPage> webPageList;
    }

@Entity(tableName = "webpage_table")
public class WebPage {
    @PrimaryKey(autoGenerate = true)
    public Integer webPageId;

    public final String name;
    public final String url;
    public String description;
    public final Integer website_Id;

    public WebPage(String name, String url, final Integer website_Id) {
        this.name = name;
        this.url = url;
        this.website_Id = website_Id;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public String getUrl() {
        return url;
    }

    public Integer getWebsiteId() {
        return website_Id;
    }
}

当我尝试使用我的网站Dao添加网站时,例如使用命令

Website website= new Website("website name", "websiteurl");
        websiteDao.insert(website);

我的应用程序崩溃。创建的每个网站的ID均为0,并且FOREIGN KEY约束失败。这是怎么了?我将主键从int更改为INTEGER,然后将主键设置为null,而当primarykey为int时,它将id设置为0。怎么了?

1 个答案:

答案 0 :(得分:0)

已解决。最后,我重新编写了代码。这次,我从https://github.com/Pavneet-Sing/RoomDemo/blob/master/app/src/main/java/com/example/pavneet_singh/roomdemo/AddNoteActivity.java

复制

具体来说,对于“插入”异步任务的DoInBackground,有代码`

protected Boolean doInBackground(Void... objs) {
            // retrieve auto incremented note id
            long j = activityReference.get().noteDatabase.getNoteDao().insertNote(note);
            note.setNote_id(j);
            Log.e("ID ", "doInBackground: "+j );
            return true;
        }

通过让Insert Dao方法返回long

 @Insert
    long insertNote(Note note);

数据库正确地自动分配了预期的ID。如果同时添加多个注释,请按照添加注释的顺序进行注释。在这种情况下,不会将id设置为每次插入到数据库的调用都会创建一个异步方法。所以我要做的是在每个异步调用之后添加一个.get(),以确保一切都按时正确设置