绿道在指定自动增量时生成一个带有 id 的构造函数



尝试为实体创建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 参数的构造函数?这就是自动增量的重点,因此您不必自己指定它。

有什么建议吗?

我使用的是旧版本的 Greendao,所以对此持保留态度。 仅当您实际将其插入数据库时,才会执行自动增量。 构造函数最初只是一个内存中的对象。 如果您还不知道,此时的 ID 应为 null。 插入对象后,您可以读取其真实 ID。

相关内容

最新更新