3

How can I make an integer or long field to be auto-incremented using annotation.

Shahidul
  • 2,997
  • 1
  • 21
  • 20

2 Answers2

1

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.

Piotr Wittchen
  • 3,853
  • 4
  • 26
  • 39
0

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.