0

I have a simple Model and I am inserting it into sqlite via GreenDao. Here is the model class:

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

}

I am trying to insert the instances of this ImageModel class via AbstractDao.insert() call, but the row id returned is always 0.

I have tried using Long instead of long for id field type as suggested in this github discussion: https://github.com/greenrobot/greenDAO/issues/441

but this does not seems to work. Any help is much appreciated.

Madeyedexter
  • 1,155
  • 2
  • 17
  • 33

1 Answers1

1

I had a similar problem. Do the following:

  1. Change long id by Long id in all tables that id is autoincrement.
  2. Delete all code generated by GreenDAO (in case that you don't use generator or you are using @annotations of GreenDAO 3), then CLEAN PROJECT.
  3. Do ALTER TABLE to all tables with id autoincrement OR re-create all tables in this way: https://stackoverflow.com/a/32429968/5286400

If you want, see the next link with a similar problem: https://stackoverflow.com/a/44527501/5286400

VIX
  • 605
  • 4
  • 15