mercredi 16 septembre 2015

Android sqlite ID not generated

I'm currently developing an App, where I'm using a database. For that I'm using the GreenDAO library. My problem now is, that it appears, that the primaryId generated by GreenDAO is not automatically generated when I try to insert a new Entry. Here are the relevant Codesnippets:

Dao Generator

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();
...

Save Method of every Entity-object

public void save() {
    if(TextUtils.isEmpty(getHash())) {
        generateHash();
    }
    if(!isAttached()) {
        DatabaseHelper.getSession().insert(this);
    }
    setLinkedHashes();
    modify();
    DatabaseHelper.getSession().update(this);
}

DatabaseHelper.getSession() returns gets the current daoSession, the hash-methods only fill/generate some hashes if necessary and are not really related.

If I now want to save the following Entity

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

I get the following error Message

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)

I'm running against a wall and can't find my error. Can someone point me in the right direction?

Aucun commentaire:

Enregistrer un commentaire