如何将主键作为Room Persistence lib的自动增量

时间:2017-05-22 09:44:54

标签: android kotlin android-room

我正在创建一个Entity(Room Persistence lib)类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自动增量字段?

8 个答案:

答案 0 :(得分:249)

您需要使用autoGenerate属性

您的主键注释应如下所示:

@PrimaryKey(autoGenerate = true)

参考here

答案 1 :(得分:71)

您可以像这样添加@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()
}

答案 2 :(得分:21)

添加@PrimaryKey(autoGenerate = true)

@Entity
public class User {

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


    @PrimaryKey(autoGenerate = true)
    private int id;

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

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

}
存储数据时

 db.userDao().InsertAll(new User(0,sName,sPhone));

在创建对象(我的案例用户对象)

时,将设为零

如果字段类型为long或int(或其TypeConverter将其转换为long或int),则插入方法会在插入项目时将0视为未设置。

如果字段的类型是Integer或Long(Object)(或者其TypeConverter将其转换为Integer或Long),则插入方法会在插入项目时将null视为未设置。

答案 3 :(得分:9)

经过这么多答案,这真是令人难以置信,但最后我做得差不多。我不希望主键可以为空,我希望将其作为第一个参数,并且还想插入而不定义它,并且也不应该是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)
}

答案 4 :(得分:4)

例如,如果您要存储.lc-buy-sm-body-wrap { min-width: 400px; max-width: 100%; position: relative; .lc-buy-sm-buy-wrap { position: absolute; width: 248px; top: -4px; left: 3px; .lc-buy-sm-buy-price-wrap { position: absolute; left: 0px; bottom: 0px; max-width: 100%; .lc-buy-sm-buy-price { position: absolute; left: 0px; display: table; padding: 4px; padding-left: 10px; padding-right: 10px; border: 4px solid @main-background-color; background-color: yellow; font-weight: bold; border-radius: 6px; min-width: 118px; } } .lc-buy-sm-buy-button-wrap { position: absolute; right: 0px; top: 6px; width: 102px; } } } 实体,并使用字段.lc-product-buy-sm-wrap { background-color: blue; } .lc-buy-sm-buy-wrap { position: relative !important; background-color: teal; height: 52px; top: unset !important; left: unset !important; margin: auto; .lc-buy-sm-buy-price-wrap { top: 0px !important; } .lc-buy-sm-buy-button-wrap { right: 0px !important; } } 并且您想要自动生成ID,则可以执行此操作。

users

会议室将自动生成并自动增加(firstname, lastname , email)字段。

答案 5 :(得分:2)

这对我有用:

@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。

答案 6 :(得分:1)

@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,
        "","","","")
}

答案 7 :(得分:1)

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

在Java中:

@PrimaryKey(autoGenerate = true)
private int id;

在科特林:

@PrimaryKey(autoGenerate = true)
var id: Int

然后,房间将自动生成并自动增加id字段。