Android Room - primary key auto-generator

I have a problem with the fact that I can’t find the documentation for Room for Android and automatically generate the primary key.

I have an entity class that looks something like this:

@Entity
public class Preference {

    @PrimaryKey(autoGenerate = true)
    private int id;

    public void setId(int id) {

        this.id = id;

    }

}

This works fine when I set the identifier manually, but when I do not set the primary key, I get an error when the primary key is zero. Looking at the auto-generated file, I do not see anywhere that it automatically increments the primary key.

So, I think my question is this: can you automatically generate the primary key of a private member using the setter, or do I need to manually auto-generate my key in the setter?

+4
1

, id - , null. Integer, , , , 0 , .

Integer, .

+4

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


All Articles