尝试为实体创建DAO并将id指定为自动增量时似乎非常奇怪。
我的代码:
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;
}
和生成的DAO文件:
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
}
因为我已经指定id为autoincrement,为什么它会生成带有id参数的构造函数?这是自动增量的重点,因此您无需自己指定。
有什么建议吗?
答案 0 :(得分:1)
我正在使用较旧版本的Greendao,所以请带上一粒盐。只有在您将其实际插入数据库时才会执行自动增量。构造函数最初只是一个内存中的对象。如果您还不知道,那么该点的ID应为null。插入对象后,您就可以读取其真实ID。