我正在使用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);
,但它一次又一次地与ID0重叠的记录。它的工作原理无法预期。
请告诉我什么是原因。我也尝试使用插入物,但是它显示出相同的结果。请帮助我陷入困境。
我已经使用了下面的使用,并且正常工作。
@Entity(nameInDb = "cities")
public class City {
@Id(autoincrement = true)
private Long id;
....
}
唯一的区别是您使用了Id
,也许将其与Capital I
一起使用,使其成为保留的单词并导致此问题。或者,也许您应该将其上方的注释向@Id
简要介绍,而不是完整的软件包路径。我知道这听起来很奇怪,但值得尝试。
我认为您错过了检查此功能。请检查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中。
使用长而不是长。这项工作