Greendao Autoincrement插入记录不起作用

时间:2018-02-26 07:35:50

标签: android greendao greendao-generator

我正在使用greendao 3.2并制作实体和数据库。一切正常。 但是我在创建必须是自动增量的Id属性时遇到了麻烦。我知道如何在SQL中使用greendao,这给了我更多艰难的时间。

我宣布我的实体正常。让我举个例子。

@Entity
public class User {

// this will make your id autoincremented
@org.greenrobot.greendao.annotation.Id (autoincrement = true)
private Long Id;
private String name;
@Generated(hash = 690585871)
public User(Long Id, String name) {
    this.Id = Id;
    this.name = name;
}
@Generated(hash = 586692638)
public User() {
}
public Long getId() {
    return this.Id;
}
public void setId(Long Id) {
    this.Id = Id;
}
public String getName() {
    return this.name;
}
public void setName(String name) {
    this.name = name;
}
}

并插入像

这样的值
user = new User();
    user.setName("Hello Green Dao");
    Long id = userDao.insertOrReplace(user);

但它的重复记录一次又一次地与Id 0.它没有按预期工作。

请告诉我是什么原因。我也尝试过使用Insert,但它显示的结果相同。请帮助我坚持下去。

3 个答案:

答案 0 :(得分:1)

我使用它如下,它工作正常。

@Entity(nameInDb = "cities")
public class City {
  @Id(autoincrement = true)
  private Long id;
  ....
}

唯一的区别是您使用Id,可能使用大写I使其成为保留字并导致此问题。或者您可以将其上方的注释简述为@Id而不是完整的包路径。我知道这听起来很奇怪,但值得尝试。

答案 1 :(得分:0)

我认为您错过了检查此功能。请检查dao

public class UserDao extends AbstractDao<User, Long> {

public static final String TABLENAME = "USER";

/**
 * Properties of entity User.<br/>
 * Can be used for QueryBuilder and for referencing column names.
 */
public static class Properties {
    public final static Property Id = new Property(0, Long.class, "Id", true, "_id");
    public final static Property Name = new Property(1, String.class, "name", false, "NAME");
}


public UserDao(DaoConfig config) {
    super(config);
}

public UserDao(DaoConfig config, DaoSession daoSession) {
    super(config, daoSession);
}

/** Creates the underlying database table. */
public static void createTable(Database db, boolean ifNotExists) {
    String constraint = ifNotExists? "IF NOT EXISTS ": "";
    db.execSQL("CREATE TABLE " + constraint + "\"USER\" (" + //
            "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: Id
            "\"NAME\" TEXT);"); // 1: name
}

/** Drops the underlying database table. */
public static void dropTable(Database db, boolean ifExists) {
    String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"USER\"";
    db.execSQL(sql);
}

@Override
protected final void bindValues(DatabaseStatement stmt, User entity) {
    stmt.clearBindings();

    Long Id = entity.getId();
    if (Id != null) {
        stmt.bindLong(1, Id);
    }

    String name = entity.getName();
    if (name != null) {
        stmt.bindString(2, name);
    }
}

@Override
protected final void bindValues(SQLiteStatement stmt, User entity) {
    stmt.clearBindings();

    Long Id = entity.getId();
    if (Id != null) {
        stmt.bindLong(1, Id);
    }

    String name = entity.getName();
    if (name != null) {
        stmt.bindString(2, name);
    }
}

@Override
public Long readKey(Cursor cursor, int offset) {
    return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
}    

@Override
public User readEntity(Cursor cursor, int offset) {
    User entity = new User( //
        cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // Id
        cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1) // name
    );
    return entity;
}

@Override
public void readEntity(Cursor cursor, User entity, int offset) {
    entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
    entity.setName(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
 }

@Override
protected final Long updateKeyAfterInsert(User entity, long rowId) {
    entity.setId(rowId);
    return rowId;
}

@Override
public Long getKey(User entity) {
    if(entity != null) {
        return entity.getId();
    } else {
        return null;
    }
}

@Override
public boolean hasKey(User entity) {
    return entity.getId() != null;
}

@Override
protected final boolean isEntityUpdateable() {
    return true;
}

}

找到整个项目here。我想你需要保留一些支票。在UserDao中。

答案 2 :(得分:-1)

使用Long代替long。这项工作对我来说