【发布时间】:2015-09-16 10:26:09
【问题描述】:
我目前正在开发一个应用程序,我正在使用数据库。为此,我正在使用 GreenDAO 库。我现在的问题是,当我尝试插入新条目时,GreenDAO 生成的 primaryId 似乎不会自动生成。下面是相关的Codesn-ps:
道生成器
Entity entity = schema.addEntity("Client");
entity.getAdditionalImportsEntity().add("path.to.package.Entity");
entity.setSuperclass("Entity");
entity.addIdProperty().autoincrement();
entity.addBooleanProperty("modified");
entity.addBooleanProperty("inactive");
entity.addStringProperty("hash").index();
...
每个实体对象的保存方法
public void save() {
if(TextUtils.isEmpty(getHash())) {
generateHash();
}
if(!isAttached()) {
DatabaseHelper.getSession().insert(this);
}
setLinkedHashes();
modify();
DatabaseHelper.getSession().update(this);
}
DatabaseHelper.getSession() 返回获取当前的 daoSession,hash-methods 仅在必要时填充/生成一些散列并且并不真正相关。
如果我现在想保存以下实体
this = {path.to.package.dao.Client@831707287832}
accuracy = null
hash = {java.lang.String@831706918568} "6993298135964198b4f135e922e7cd8b"
id = null
inactive = null
lastSeen = null
modified = null
nickname = {java.lang.String@831707287888} "Test"
status = null
我收到以下错误消息
android.database.sqlite.SQLiteConstraintException: CLIENT.CLIENT_ID may not be NULL (code 19)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at de.greenrobot.dao.AbstractDao.executeInsert(AbstractDao.java:348)
at de.greenrobot.dao.AbstractDao.insert(AbstractDao.java:293)
at de.greenrobot.dao.AbstractDaoSession.insert(AbstractDaoSession.java:63)
at path.to.package.dao.Entity.save(Entity.java:48)
at path.to.package.utils.DatabaseHelper.getSelf(DatabaseHelper.java:76)
at path.to.package.updateService.LocationService$LocationListener.onLocationChanged(LocationService.java:40)
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:279)
at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:208)
at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:224)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
我正在撞墙,找不到我的错误。有人能指出我正确的方向吗?
【问题讨论】: