277

我正在创建一个实体(房间持久性库)类 Food,我想在其中创建foodId自动增量。

@Entity
class Food(var foodName: String, var foodDesc: String, var protein: Double, var carbs: Double, var fat: Double)
{
    @PrimaryKey
    var foodId: Int = 0
    var calories: Double = 0.toDouble()
}

如何设置foodId自动增量字段?

4

9 回答 9

487

您需要使用该autoGenerate属性

您的主键注释应该是这样的:

@PrimaryKey(autoGenerate = true)

PrimaryKey的参考。

于 2017-05-22T09:51:12.843 回答
176

你可以@PrimaryKey(autoGenerate = true)这样添加:

@Entity
data class Food(
        var foodName: String, 
        var foodDesc: String, 
        var protein: Double, 
        var carbs: Double, 
        var fat: Double
){
    @PrimaryKey(autoGenerate = true)
    var foodId: Int = 0 // or foodId: Int? = null
    var calories: Double = 0.toDouble()
}
于 2017-10-15T10:01:44.397 回答
62

add @PrimaryKey(autoGenerate = true)

@Entity
public class User {

    @PrimaryKey(autoGenerate = true)
    private int id;

    @ColumnInfo(name = "full_name")
    private String name;

    @ColumnInfo(name = "phone")
    private String phone;

    public User(){
    }

    //type-1
    public User(String name, String phone) {
        this.name = name;
        this.phone = phone;
    }

    //type-2
    public User(int id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

}

while storing data

 //type-1
 db.userDao().InsertAll(new User(sName,sPhone)); 

 //type-2
 db.userDao().InsertAll(new User(0,sName,sPhone)); 

type-1

If you are not passing value for primary key, by default it will 0 or null.

type-2

Put null or zero for the id while creating object (my case user object)

If the field type is long or int (or its TypeConverter converts it to a long or int), Insert methods treat 0 as not-set while inserting the item.

If the field's type is Integer or Long (Object) (or its TypeConverter converts it to an Integer or a Long), Insert methods treat null as not-set while inserting the item.

于 2018-04-28T17:24:45.323 回答
44

经过这么多答案,这令人难以置信,但我最终还是做了一点不同。我不喜欢主键可以为空,我想把它作为第一个参数,也想插入而不定义它,它也不应该是 var。

@Entity(tableName = "employments")
data class Employment(
    @PrimaryKey(autoGenerate = true) val id: Long,
    @ColumnInfo(name = "code") val code: String,
    @ColumnInfo(name = "title") val name: String
){
    constructor(code: String, name: String) : this(0, code, name)
}
于 2020-08-17T22:07:17.753 回答
17

这对我有用:

@Entity(tableName = "note_table")
data class Note(
    @ColumnInfo(name="title") var title: String,
    @ColumnInfo(name="description") var description: String = "",
    @ColumnInfo(name="priority") var priority: Int,
    @PrimaryKey(autoGenerate = true) var id: Int = 0//last so that we don't have to pass an ID value or named arguments
)

请注意,在将实体插入到 Room 之前,id 是最后一个,以避免在创建实体时使用命名参数。将其添加到房间后,在更新实体时使用 id。

于 2020-08-09T21:27:38.347 回答
7

例如,如果您有一个users要存储的实体,带有字段(firstname, lastname , email)并且您想要自动生成的 id,您可以这样做。

@Entity(tableName = "users")
data class Users(
   @PrimaryKey(autoGenerate = true)
   val id: Long,
   val firstname: String,
   val lastname: String,
   val email: String
)

Room 然后将自动生成并自动增加该id字段。

于 2017-09-09T03:49:49.360 回答
6
@Entity(tableName = "user")
data class User(

@PrimaryKey(autoGenerate = true)  var id: Int?,
       var name: String,
       var dob: String,
       var address: String,
       var gender: String
)
{
    constructor():this(null,
        "","","","")
}
于 2019-06-05T06:13:57.523 回答
0

在下面的示例中,当您创建新用户时,按构造函数中的方式传递参数。Room 会自动生成 id。所有用户对象 id 都已在 id setter 中设置为 int 默认值,因此不要调用 setId

@Entity
public class User {

    @PrimaryKey(autoGenerate = true)
    private int id;

    @ColumnInfo(name = "full_name")
    private String name;

    @ColumnInfo(name = "phone")
    private String phone;

   
    public User(String name, String phone) {
        this.name = name;
        this.phone = phone;
    }

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

}
于 2022-01-22T13:24:29.940 回答
0

使用下面的代码注释您的 Entity 类。

在 Java 中:

@PrimaryKey(autoGenerate = true)
private int id;

在科特林:

@PrimaryKey(autoGenerate = true)
var id: Int

Room 将自动生成并自动增加 id 字段。

于 2020-01-30T07:30:08.803 回答