Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORMLite id generate

I have this object:

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

If I make an object like this:

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

And then if i make this:

int id = newSet.getID();

Will I get the set id or should I get the whole object from the database with

setDao.queryForEq("setName", "name");
like image 779
nikmin Avatar asked Nov 09 '12 09:11

nikmin


1 Answers

So the ORMLite documentation is pretty extensive. If you look up generated-id in the docs you'd see the following statement:

Boolean whether the field is an auto-generated id field. Default is false. Only one field can have this set in a class. This tells the database to auto-generate a corresponding id for every row inserted. When an object with a generated-id is created using the Dao.create() method, the database will generate an id for the row which will be returned and set in the object by the create method.

So after you call setDao.create(newSet), the newSet object's setID field will be changed with the id from the database.

Also, calling a field setID is a very bad pattern. I'd recommend just use id.

like image 143
Gray Avatar answered Nov 07 '22 06:11

Gray