Stay organized with collections
Save and categorize content based on your preferences.
@Target(allowedTargets = [AnnotationTarget.FUNCTION])
@Retention(value = AnnotationRetention.BINARY)
public annotation Upsert
Marks a method in a Dao
annotated class as an upsert (insert or update) method.
The implementation of the method will insert its parameters into the database if it does not already exists (checked by primary key). If it already exists, it will update its parameters in the database.
All of the parameters of the upsert method must either be classes annotated with Entity
or collections/array of it.
Example:
@Dao
interface MusicDao {
@Upsert
fun upsertSongs(varargs songs: Song)
@Upsert
fun upsertBoth(song1: Song, song2: Song)
@Upsert
fun upsertAlbumWithSongs(album: Album, songs: List<Song>)
}
If the target entity is specified via entity
then the parameters can be of arbitrary POJO types that will be interpreted as partial entities. For example:
@Entity
data class Playlist (
@PrimaryKey(autoGenerate = true)
val playlistId: Long,
val name: String,
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 NameAndDescription (
val name: String,
val description: String
)
@Dao
interface PlaylistDao {
@Upsert(entity = Playlist::class)
fun upsertNewPlaylist(nameDescription: NameAndDescription)
}
Summary
Public methods
public final @NonNull KClass<@NonNull ?> getEntity()
The target entity of the upsert method.
When this is declared, the upsert method parameters are interpreted as partial entities when the type of the parameter differs from the target. The POJO class that represents the entity must contain all of the non-null fields without default values of the target entity.
If the target entity contains a PrimaryKey
that is auto generated, then the POJO class doesn't need an equal primary key field, otherwise primary keys must also be present in the POJO. If the primary key already exists, only the columns represented by the partial entity fields will be updated
By default the target entity is interpreted by the method parameters.
Returns |
@NonNull KClass<@NonNull ?> |
the target entity of the upsert method or none if the method should use the parameter type entities.
|
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-08-13 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-13 UTC."],[],[],null,["# Upsert\n======\n\nArtifact: [androidx.room:room-common](/jetpack/androidx/releases/room) \n[View Source](https://cs.android.com/search?q=file:androidx/room/Upsert.kt+class:androidx.room.Upsert) \nAdded in [2.5.0](/jetpack/androidx/releases/room#2.5.0)\n\n*** ** * ** ***\n\n[Kotlin](/reference/kotlin/androidx/room/Upsert \"View this page in Kotlin\") \\|Java\n\n\n```\n@Target(allowedTargets = [AnnotationTarget.FUNCTION])\n@Retention(value = AnnotationRetention.BINARY)\npublic annotation Upsert\n```\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nMarks a method in a [Dao](/reference/androidx/room/Dao) annotated class as an upsert (insert or update) method.\n\nThe implementation of the method will insert its parameters into the database if it does not already exists (checked by primary key). If it already exists, it will update its parameters in the database.\n\nAll of the parameters of the upsert method must either be classes annotated with [Entity](/reference/androidx/room/Entity) or collections/array of it.\n\nExample: \n\n```gdscript\n@Dao\ninterface MusicDao {\n @Upsert\n fun upsertSongs(varargs songs: Song)\n\n @Upsert\n fun upsertBoth(song1: Song, song2: Song)\n\n @Upsert\n fun upsertAlbumWithSongs(album: Album, songs: List\u003cSong\u003e)\n}\n```\n\nIf the target entity is specified via [entity](/reference/androidx/room/Upsert#entity()) then the parameters can be of arbitrary POJO types that will be interpreted as partial entities. For example: \n\n```scalate-server-page\n@Entity\ndata class Playlist (\n @PrimaryKey(autoGenerate = true)\n val playlistId: Long,\n val name: String,\n val description: String?,\n\n @ColumnInfo(defaultValue = \"normal\")\n val category: String,\n\n @ColumnInfo(defaultValue = \"CURRENT_TIMESTAMP\")\n val createdTime: String,\n\n @ColumnInfo(defaultValue = \"CURRENT_TIMESTAMP\")\n val lastModifiedTime: String\n)\n\ndata class NameAndDescription (\n val name: String,\n val description: String\n)\n\n@Dao\ninterface PlaylistDao {\n @Upsert(entity = Playlist::class)\n fun upsertNewPlaylist(nameDescription: NameAndDescription)\n}\n``` \n\n| See also |\n|-------------------------------------------|---|\n| [Insert](/reference/androidx/room/Insert) | |\n| [Update](/reference/androidx/room/Update) | |\n\nSummary\n-------\n\n| ### Public constructors |\n|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [Upsert](/reference/androidx/room/Upsert#Upsert(kotlin.reflect.KClass))`(@`[NonNull](/reference/androidx/annotation/NonNull)` `[KClass](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html)`\u003c@`[NonNull](/reference/androidx/annotation/NonNull)` ?\u003e entity)` |\n\n| ### Public methods |\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------|\n| `final @`[NonNull](/reference/androidx/annotation/NonNull)` `[KClass](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html)`\u003c@`[NonNull](/reference/androidx/annotation/NonNull)` ?\u003e` | [getEntity](/reference/androidx/room/Upsert#getEntity())`()` The target entity of the upsert method. |\n\nPublic constructors\n-------------------\n\n### Upsert\n\nAdded in [2.8.0-rc01](/jetpack/androidx/releases/room#2.8.0-rc01) \n\n```\npublic Upsert(@NonNull KClass\u003c@NonNull ?\u003e entity)\n``` \n\nPublic methods\n--------------\n\n### getEntity\n\n```\npublic final @NonNull KClass\u003c@NonNull ?\u003e getEntity()\n```\n\nThe target entity of the upsert method.\n\nWhen this is declared, the upsert method parameters are interpreted as partial entities when the type of the parameter differs from the target. The POJO class that represents the entity must contain all of the non-null fields without default values of the target entity.\n\nIf the target entity contains a [PrimaryKey](/reference/androidx/room/PrimaryKey) that is auto generated, then the POJO class doesn't need an equal primary key field, otherwise primary keys must also be present in the POJO. If the primary key already exists, only the columns represented by the partial entity fields will be updated\n\nBy default the target entity is interpreted by the method parameters. \n\n| Returns |\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------|\n| `@`[NonNull](/reference/androidx/annotation/NonNull)` `[KClass](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html)`\u003c@`[NonNull](/reference/androidx/annotation/NonNull)` ?\u003e` | the target entity of the upsert method or none if the method should use the parameter type entities. |"]]