绿道插入不生成id

时间:2017-05-21 04:05:00

标签: android sqlite greendao

我有一个简单的模型,我通过GreenDao将它插入到sqlite中。这是模型类:

@Entity
public class ImageModel implements Parcelable {

    public static final Creator<ImageModel> CREATOR = new Creator<ImageModel>() {
        @Override
        public ImageModel createFromParcel(Parcel in) {
            return new ImageModel(in);
        }

        @Override
        public ImageModel[] newArray(int size) {
            return new ImageModel[size];
        }
    };
    @Id(autoincrement = true)
    private long id;
    private String downloadURL;
    private String pasteId;
    private String storageLocation;


    protected ImageModel(Parcel in) {
        id = in.readLong();
        downloadURL = in.readString();
        pasteId = in.readString();
        storageLocation = in.readString();
    }


    @Generated(hash = 345376828)
    public ImageModel(long id, String downloadURL, String pasteId,
                      String storageLocation) {
        this.id = id;
        this.downloadURL = downloadURL;
        this.pasteId = pasteId;
        this.storageLocation = storageLocation;
    }


    @Generated(hash = 799163379)
    public ImageModel() {
    }

    @Override
    public String toString() {
        return "ImageModel{" +
                "id=" + id +
                ", downloadURL='" + downloadURL + '\'' +
                ", pasteId='" + pasteId + '\'' +
                ", storageLocation='" + storageLocation + '\'' +
                '}';
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(id);
        dest.writeString(downloadURL);
        dest.writeString(pasteId);
        dest.writeString(storageLocation);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public String getDownloadURL() {
        return this.downloadURL;
    }

    public void setDownloadURL(String downloadURL) {
        this.downloadURL = downloadURL;
    }

    public String getPasteId() {
        return this.pasteId;
    }

    public void setPasteId(String pasteId) {
        this.pasteId = pasteId;
    }

    public String getStorageLocation() {
        return this.storageLocation;
    }

    public void setStorageLocation(String storageLocation) {
        this.storageLocation = storageLocation;
    }

    public long getId() {
        return this.id;
    }

    public void setId(long id) {
        this.id = id;
    }

}

我试图通过AbstractDao.insert()调用插入此ImageModel类的实例,但返回的行id始终为0.

我尝试使用Long代替long作为id字段类型,如本github讨论中所建议的那样: https://github.com/greenrobot/greenDAO/issues/441

但这似乎不起作用。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我有类似的问题。执行以下操作:

  1. 在ID为自动增量的所有表格中,按long id更改Long id
  2. 删除GreenDAO生成的所有代码(如果您不使用生成器或使用GreenDAO的@annotations 3),则清除项目。
  3. 对{id>自动增量的所有表执行ALTER TABLE以这种方式重新创建所有表格:https://stackoverflow.com/a/32429968/5286400
  4. 如果需要,请参阅下一个包含类似问题的链接:https://stackoverflow.com/a/44527501/5286400