最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - AutoIncrement of Room Entity Id is not working - Stack Overflow

programmeradmin1浏览0评论

My app is currently using retrofit and Room but I am struggling to find a way to autogenerate the id of the Db object. When mapping retrofit object to the RoomDb entity, it keeps asking for an id to filed up even if I added the autogenerate key

Below is the retrofit object:

data class RetrofitProduct(
    @SerializedName("id")
    val id: String,
    @SerializedName("types")
    val types: List<String>,
    @SerializedName("date")
    val date: String,
)

and the associated Room Entity:

@Entity
data class ProductEntity(
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    val id: Int,
    @ColumnInfo(name = "productId")
    val productId: String,
    @ColumnInfo(name = "types")
    val types: List<String>,
)

the Mapper is as below:

fun RetrofitProduct.toEntity(): ProductEntity {
    return ProductEntity(
        productId = this.id,
        types = this.types,
    )
}

But at build time, the compiler complains that I need to add the id:

No value passed for parameter 'id'

I want this id to be generated by Room as a way to get the number of items for my paging.

Basically if I pull the lastItem, I know that with the id is for example: 25. So I need to ask for 26 and more.

My app is currently using retrofit and Room but I am struggling to find a way to autogenerate the id of the Db object. When mapping retrofit object to the RoomDb entity, it keeps asking for an id to filed up even if I added the autogenerate key

Below is the retrofit object:

data class RetrofitProduct(
    @SerializedName("id")
    val id: String,
    @SerializedName("types")
    val types: List<String>,
    @SerializedName("date")
    val date: String,
)

and the associated Room Entity:

@Entity
data class ProductEntity(
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    val id: Int,
    @ColumnInfo(name = "productId")
    val productId: String,
    @ColumnInfo(name = "types")
    val types: List<String>,
)

the Mapper is as below:

fun RetrofitProduct.toEntity(): ProductEntity {
    return ProductEntity(
        productId = this.id,
        types = this.types,
    )
}

But at build time, the compiler complains that I need to add the id:

No value passed for parameter 'id'

I want this id to be generated by Room as a way to get the number of items for my paging.

Basically if I pull the lastItem, I know that with the id is for example: 25. So I need to ask for 26 and more.

Share Improve this question edited Feb 3 at 21:58 tyg 16.2k4 gold badges36 silver badges48 bronze badges asked Feb 3 at 21:39 SebSeb 3,2255 gold badges39 silver badges86 bronze badges 2
  • What happens if you provide any id, like 0 for example? – tyg Commented Feb 3 at 21:59
  • Just add a default value of 0 for id and it will be fixed. – Masoud Karimi Commented Feb 4 at 6:01
Add a comment  | 

1 Answer 1

Reset to default 3

This is actually a pretty common gotcha when working with Room and auto-generated IDs. The problem is that even though you've marked the ID as autoGenerate = true, Kotlin still requires all parameters to be provided in the constructor since ProductEntity is a data class. Here are a couple of ways to solve this:

The quickest fix - provide a default value of 0:

kotlinCopy@Entity
data class ProductEntity(
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    val id: Int = 0,  // Add this default value
    @ColumnInfo(name = "productId")
    val productId: String,
    @ColumnInfo(name = "types")
    val types: List<String>,
)

Or if you prefer being more explicit in your mapper:

kotlinCopyfun RetrofitProduct.toEntity(): ProductEntity {
    return ProductEntity(
        id = 0,  // Room will ignore this and auto-generate
        productId = this.id,
        types = this.types,
    )
}

Room will ignore the 0 value and auto-generate a new ID when you insert the entity. This works because Room checks for 0 or null (depending on if you're using nullable types) as signals to auto-generate the ID. When you use this approach for paging, you can still query the last item's ID just fine since Room will have replaced that 0 with the actual auto-generated value.

发布评论

评论列表(0)

  1. 暂无评论