It seems very weird issue when trying to create the DAO for an Entity and have specified the id as autoincrement.
My code:
private static Entity addSearch(Schema schema) {
Entity search = schema.addEntity("Search");
search.addIdProperty().primaryKey().autoincrement();
search.addStringProperty("title").notNull();
search.addIntProperty("type");
search.addIntProperty("timestamp");
return search;
}
and the generated DAO file:
public class Search {
private Long id;
/** Not-null value. */
private String title;
private Integer type;
private Integer timestamp;
// KEEP FIELDS - put your custom fields here
// KEEP FIELDS END
public SearchData() {
}
public SearchData(Long id) {
this.id = id;
}
public SearchData(Long id, String title, Integer type, Integer timestamp) {
this.id = id;
this.title = title;
this.type = type;
this.timestamp = timestamp;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
/** Not-null value. */
public String getTitle() {
return title;
}
/** Not-null value; ensure this value is available before it is saved to the database. */
public void setTitle(String title) {
this.title = title;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public Integer getTimestamp() {
return timestamp;
}
public void setTimestamp(Integer timestamp) {
this.timestamp = timestamp;
}
// KEEP METHODS - put your custom methods here
// KEEP METHODS END
}
since i have specified the id to be autoincrement why does it generate constructor's with id argument? That is the point of autoincrement so that you do not have to specify it by yourself.
Any suggestions?
I'm on an older version of Greendao, so take this with a grain of salt. The autoincrement is only done when you actually insert it into the database. The constructor is just an in-memory object at first. The ID at that point should be null if you don't know it yet. Once you insert the object, then you can read its real ID.