GreenDao自增插入记录不起作用。

4

我正在使用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;
}
}

and inserting values like

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

但是它不断地与ID为0的记录重叠。它并没有按预期工作。 请告诉我原因。我已经尝试使用Insert,但结果相同。请帮忙,我卡在这里了。
3个回答

1
我已经按照下面的方式使用它,并且它运行良好。
@Entity(nameInDb = "cities")
public class City {
  @Id(autoincrement = true)
  private Long id;
  ....
}

唯一的区别是你使用了 Id,也许使用大写的 I 使其成为了保留字并引起了这个问题。或者也许你应该将其上面的注解简化为 @Id 而不是完整的包路径。我知道这听起来很奇怪,但值得一试。

1
是的,它已经插入了。例如,当插入一个记录后,再插入另一个记录时,它会替换第一个记录。 - Android teem
建议使用 Long。 - Android teem
我本来没有得到自动递增,但是我按照@shararti KAKA的建议更改了dao。请查看他的答案,你就会知道我做了什么更改。 - Android teem
清理您的项目并删除由greenDao在您的模型中生成的所有代码,然后运行gradle build以便重新创建它们。然后检查dao是否正确。我猜应该没问题了。 - Akram
不,我正在使用生成器。不是你说的那种方法来构建项目。要创建DAO,我必须运行生成器。 - Android teem
显示剩余6条评论

0

使用 Long 代替 long。这对我有用。


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;
}

}

整个项目可以在这里找到。我认为你需要在UserDao中进行一些检查。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接