Greendao Autoincrement inserting record not working

2.1k views Asked by At

I am using greendao 3.2 and making Entities and database. Its all working fine. But I am having trouble when creating Id property that must be Auto increment. I know How to do it in SQL but using greendao its giving me much more tough time.

I have declared my entities as normal. let me give you example.

@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);

but its overlapping record again and again with Id 0. Its not working as expected.

please tell me what is the reason. I have tried using Insert also but it is showing same results. Please help I am stuck into it.

3

There are 3 answers

11
Akram On

I have used it as below and it is working fine.

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

The only difference is that you used Id, maybe using it with capital I made it kind of reserved word and cause this problem. Or maybe you should brief the annotation above it to @Id instead of full package path. I know this all sounds weird, but it worth to try.

0
A.s.ALI On

I think you missed out checking this functionality. Please check the 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;
}

}

this whole project is found here. I think you need to keep some check like it. in the UserDao.

0
Siyamand Akhtea On

use Long Instead of long . this work for me