Android / ORMLite Insert row with id

I am currently using ORMLite to work with a SQLite database on Android. As part of this, I download a bunch of data from the server server, and I would like this data to be added to the SQLite database in the same format as on the backend server (that is, the identifiers are the same, etc.).

So, my question is for you: if I fill out my database entry object (we will call it โ€œHardwareโ€), including the field generated by the generated kernel / primary key via setId (), and then I run DAO.create () with will the equipment input be saved correctly? I tried it this way and it seems to me that it is not. If so, I will try again and look for other problems, but with the first few passes over the code I could not find it. In fact, if I call DAO.create () in the database object with an identifier, will this ID be sent to the database, and if not, how can I insert a row with the primary key value already filled in?

Thanks!

+6
source share
2 answers

@Femi is correct that an object can be either a generated identifier or an identifier, but not both. The problem is that ORMLite stores the object, but it must also match the schema with which the database was created.

ORMLite supports the allowGeneratedIdInsert=true parameter for the @DatabaseField annotation, which allows this behavior. This is not supported by some types of databases (e.g. Derby), but it runs on Android / SQLite.

For posterity, you can also create 2 objects that use the same table - one with the generated identifier and without it. You can then insert the generated Dao identifier to get this behavior, and another Tao to accept the id value set by the caller. Here 's another answer talking about this . The problem for you is that it will create many additional DAOs.

The only other solution is not to use an identifier for your purposes. Let the database generate an identifier, and then add an additional field that you use for external purposes. Forcing a database identifier under certain circumstances seems like a bad model to me.

+13
source

From http://ormlite.com/docs/generated-id :

Logical whether the field is an automatically generated identifier field. The default value is false. Only one field can have this set in the class. This tells the database to automatically generate the appropriate identifier for each row inserted. When an object with a generated identifier is created using the Dao.create () method, the database will generate an identifier for the row that will be returned and set in the object using the create method. Some databases require a sequence for the generated identifiers, in which case the sequence name will be automatically generated. To specify a sequence name, use the generated identifier. You can specify only one of this, id and the generated identifier.

You should use either generatedId (in which case it should display all identifiers) or id (in this case you can set them), but not both.

+4
source

Source: https://habr.com/ru/post/895113/


All Articles