【问题标题】:Room SQL autogenerate primary key returning 0 or nullRoom SQL 自动生成主键返回 0 或 null
【发布时间】:2019-02-07 00:36:59
【问题描述】:

所以我的数据库正在使用一个网站实体

 @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);

我的应用崩溃了。创建的每个网站都被赋予 0 的 id,并且 FOREIGN KEY 约束失败。这里出了什么问题?我将primarykey从int更改为INTEGER,当我这样做时,它将主键设置为null,当primarykey为int时,它将id设置为0。这是怎么回事?

【问题讨论】:

  • 将主键数据类型从 Integer 更改为 int 并检查,它应该可以工作
  • @pinakin 就是这样,不是。在它停止工作后,我已经改为整数。我的代码适用于一个实体的 int 主键。当我添加第二个实体时,我现在面临一个外键约束失败,因为所有自动生成的 ID 都是 0。

标签: android mysql android-room


【解决方案1】:

解决了。最后我重做了我的代码。这次是从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() 以确保一切都按时正确设置

【讨论】:

    猜你喜欢
    • 2019-06-07
    • 2020-05-19
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    • 1970-01-01
    相关资源
    最近更新 更多