Home > Enterprise >  Duh! Manually supply ID to Dao in Entity class
Duh! Manually supply ID to Dao in Entity class

Time:10-05

My entity:

@Entity(tableName = "accounts")
data class Account(
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "user_id")
    val id: Int,
    @ColumnInfo(name = "first_name")
    val firstName: String,
    @ColumnInfo(name = "last_name")
    val lastName: String?,
    @ColumnInfo(name = "email")
    val email: String
)

I am doing this:

fun register(email: String, name: String) {
    return dbRepository.createAccount(Account(firstName = name, email = email))
}

Dao:

 @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun insertAccount(account: Account) : Long

Problem: Requires me to add an ID via parameter to the Account object, annoying. Because I have annotated that user_id is @PrimaryKey(autoGenerate = true) and shouldn't be manually supplied.

CodePudding user response:

You need to set id as var and give it 0 as default value:

@Entity(tableName = "accounts")
data class Account(
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "user_id")
    var id: Int = 0,
    @ColumnInfo(name = "first_name")
    val firstName: String,
    @ColumnInfo(name = "last_name")
    val lastName: String?,
    @ColumnInfo(name = "email")
    val email: String
)

The default value is required to be able to create Account without providing the id because autoGenerate = true is not enough for the compiler to know that id is not required, and changing val to var because room in the background is going to parse that data and change that id so it must be mutable.

Note: This is room documentation for autoGenerate: 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 (or its TypeConverter converts it to an Integer or a Long), Insert methods treat null as not-set while inserting the item.

So that's why the default value should be exactly 0 or null because room treats 0 and null as not-set and replace it with autoGenerated value.

  • Related