# Update

Artifact: [androidx.room3:room3-common](https://developer.android.com/jetpack/androidx/releases/room3) [View Source](https://cs.android.com/search?q=file:androidx/room3/Update.kt+class:androidx.room3.Update) Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/room3#3.0.0)

*** ** * ** ***

Common/AllAndroid/JVMNative/C/iOSJavaScript
- Cmn

  ```
  @Target(allowedTargets = [AnnotationTarget.FUNCTION])
  @Retention(value = AnnotationRetention.BINARY)
  annotation Update
  ```

*** ** * ** ***

Marks a function in a `https://developer.android.com/reference/kotlin/androidx/room3/Dao` annotated class as an update function.

The implementation of the function will update its parameters in the database if they already exists (checked by primary keys). If they don't already exist, this option will not change the database.

All the parameters of the update function must either be classes annotated with `https://developer.android.com/reference/kotlin/androidx/room3/Entity` or collections / array of it.

Example:

```kotlin
@Dao
interface MusicDao {
    @Update
    suspend fun updateSong(song: Song)

    @Update
    suspend fun updateSongs(songs: List<Song>): Int
}
```

The function may have a return type of `Int` indicating the amount of items updated by the operation.

If a target entity is specified via `https://developer.android.com/reference/kotlin/androidx/room3/Update#entity()` value then the parameters can be of arbitrary data object types that will be interpreted as partial entities. For example:

```kotlin
@Entity
data class Playlist(
    @PrimaryKey(autoGenerate = true)
    val playlistId: Long,
    val name: String,
    @ColumnInfo(defaultValue = "")
    val description: String,
    @ColumnInfo(defaultValue = "normal")
    val category: String,
    @ColumnInfo(defaultValue = "CURRENT_TIMESTAMP")
    val createdTime: String,
    @ColumnInfo(defaultValue = "CURRENT_TIMESTAMP")
    val lastModifiedTime: String
)

data class PlaylistCategory(
    val playlistId: Long,
    val category: String,
    val lastModifiedTime: String
)

@Dao
interface PlaylistDao {
    @Update(entity = Playlist::class)
    suspend fun updateCategory(varargs category: Category)
}
```

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/room3/Delete` |   |
| `https://developer.android.com/reference/kotlin/androidx/room3/Insert` |   |
| `https://developer.android.com/reference/kotlin/androidx/room3/Upsert` |   |

## Summary

| ### Public constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/room3/Update#Update(kotlin.reflect.KClass,kotlin.Int)(entity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<*>, onConflict: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` | Cmn |

| ### Public properties |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<*>` | `https://developer.android.com/reference/kotlin/androidx/room3/Update#entity()` The target entity of the update function. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/room3/Update#onConflict()` What to do if a conflict happens. | Cmn |

## Public constructors

### Update

Cmn Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/room3#3.0.0)

```
Update(
    entity: KClass<*> = Any::class,
    onConflict: Int = OnConflictStrategy.ABORT
)
```

## Public properties

### entity

Cmn

```
val entity: KClass<*>
```

The target entity of the update function.

When this is declared, the update function parameters are interpreted as partial entities when the type of the parameter differs from the target. The data object class that represents the entity must contain a subset of the properties of the target entity along with its primary keys.

Only the columns represented by the partial entity properties will be updated if an entity with equal primary key is found.

By default, the target entity is interpreted by the function parameters.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<*>` | the target entity of the update function or none if the function should use the parameter type entities. |

### onConflict

Cmn

```
val onConflict: Int
```

What to do if a conflict happens.

Use `https://developer.android.com/reference/kotlin/androidx/room3/OnConflictStrategy#ABORT()` (default) to roll back the transaction on conflict. Use `https://developer.android.com/reference/kotlin/androidx/room3/OnConflictStrategy#REPLACE()` to replace the existing rows with the new rows. Use `https://developer.android.com/reference/kotlin/androidx/room3/OnConflictStrategy#IGNORE()` to keep the existing rows.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | How to handle conflicts. Defaults to `https://developer.android.com/reference/kotlin/androidx/room3/OnConflictStrategy#ABORT()`. |