How to make primary key as autoincrement for Room Persistence lib

前端 未结 8 1358
無奈伤痛
無奈伤痛 2020-12-07 11:55

I am creating an Entity (Room Persistence Library) class Food, where I want to make foodId as autoincrement.

@Entity
class Food(var foodName: Str         


        
8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-07 12:31

    Annotate your Entity class with the code below.

    In Java:

    @PrimaryKey(autoGenerate = true)
    private int id;
    

    In Kotlin:

    @PrimaryKey(autoGenerate = true)
    var id: Int
    

    Room will then auto-generate and auto-increment the id field.

提交回复
热议问题