ORMLite ID

I have this object:

public class Set { @DatabaseField(columnName = "setID", generatedId = true) private int setID; @DatabaseField(columnName = "setName") private String setName; } 

If I create an object like this:

 Set newSet = new Set("name"); setDao.create(newSet); 

And if I do this:

 int id = newSet.getID(); 

I will get the set id or should get the whole object from the database using

 setDao.queryForEq("setName", "name"); 
+4
source share
1 answer

Thus, the ORMLite documentation is quite extensive. If you look at the generated identifier in the documents , you will see the following statement:

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.

Therefore, after calling setDao.create(newSet) the newSet object setID will be changed with the identifier from the database.

Also, calling the setID field is a very bad template. I would recommend just using id .

+10
source

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


All Articles