GreenDao 在指定自增时生成一个带有 id 的构造函数

GreenDao generates a constructor with id when auto increment is specified

尝试为实体创建 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 是自动递增的,为什么它会生成带有 id 参数的构造函数?这就是autoincrement的意义所在,这样你就不用自己指定了。

有什么建议吗?

我使用的是旧版本的 Greendao,因此请对此持保留态度。自动增量仅在您实际将其插入数据库时​​完成。构造函数最初只是一个内存中的对象。如果您还不知道,此时的 ID 应该为空。一旦你插入对象,你就可以读取它的真实ID。