How can I make an integer or long field to be auto-incremented using annotation.
-
auto incremented under what trigger? new class creation, function call,...? – user_4685247 Jun 07 '15 at 10:32
-
when we call save method of a new instance of Model subclass – Shahidul Jun 07 '15 at 10:38
2 Answers
As we can read in a documentation:
One important thing to note is that ActiveAndroid creates an id field for your tables. This field is an auto-incrementing primary key.
Maybe accessing auto-generated primary key will be enough for you?
Moreover, if you would like to create custom primary key in you model, you can check solution mentioned in GitHub issue connected with ActiveAndroid, which looks like this:
@Table(name = "Items", id = "clientId")
public class Item extends Model {
@Column(name = "id")
private long id;
}
Then, id field is custom primary key, which will be auto-incremented.

- 3,853
- 4
- 26
- 39
In case of ActiveAndroid ORM you do not need to write id column in model, It will automatic generate auto incremented value and you can simply use it. I am giving a sample model below-
@Table(name="Items")
public class Item extends Model{
@Column(name="name")
public String name;
}
Instead of
@Table(name="Items")
public class Item extends Model{
@Column(name="Id")
public long id;
@Column(name="name")
public String name;
}
If item is an object of Item then you can simply get id by using
item.getId();
So, the correct model is first one. For reference you can click here.

- 423
- 1
- 3
- 12