Frage

There was some confusion in our team about the creation of a new database object.

The greendao orm generator creates a standard constructor that takes an id as the first value, if an idproperty was created. For most of our databases we don't want to create the id on our own and simply let the database create the id.

At the moment most of the team members pass -1L as an id. This seems to work.

I couldn't find a documented way to create a new object without an id.

What is the correct way to create such an object and in wich cases is a new id generated during the insertion in the database?

War es hilfreich?

Lösung

just a quick look at the docs for Greendao reveals:

Now have a look at the addNote method, how you insert a new note in the database:

Note note = new Note(null, noteText, comment, new Date());
noteDao.insert(note);
Log.d("DaoExample", "Inserted new note, ID: " + note.getId());

Just create a Java object and call insert on the DAO. When the insert method returns, the database id of the just inserted note is already assigned to the object, as you can see in the log statment.

looks like null is the answer you want.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top