andtompoi
andtompoi

Reputation: 59

Room. autoGenerate PrimaryKey

When autogenerated in PrimaryKey I have in add first record int is 1, I want the first to be 0, when I give setId(0) the first is also 1.

And my second question, how to implement that when starting the application, if there is no id = 0 record in database, to automatically do a default insert.

model:

@Entity(tableName = "group_table")
public class Group {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    private int id;

    public void setId(int id) {
        this.id = id;
     }
}

Upvotes: 0

Views: 40

Answers (1)

anish sharma
anish sharma

Reputation: 590

Insert methods treat 0 as not-set while inserting the item for primitive types like int, so use Integer instead of int and then call setId(0) it will work. While starting the application make a call to a method. This method will be called only once when your application is starting and within this method check database f there is no id = 0 record in database, make an insert for 0 id.

Upvotes: 1

Related Questions