Upsert Oracle AI Database

An upsert is a database operation that will update an existing row if a specified value already exists in a table, and insert a new row if the specified value doesn’t already exist.

Authors: Sergio del Amo

Micronaut Version: 5.2.3

1. Getting Started

In this guide, we will create a Micronaut application written in Kotlin.

An upsert is a native database operation that inserts a row when no matching row exists, or updates the existing row when a match is found. Doing this in a single statement, instead of a "find, then insert or update" sequence, avoids a race condition: two concurrent requests cannot both decide the row is missing and both attempt an insert.

In this guide, you will declare an upsert method in a Micronaut Data JDBC repository and use it from an HTTP controller backed by Oracle AI Database.

2. What you will need

To complete this guide, you will need the following:

3. Solution

We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.

4. Writing the Application

Create an application using the Micronaut Command Line Interface or with Micronaut Launch.

mn create-app example.micronaut.micronautguide \
    --features=data-jdbc,liquibase,oracle,serialization-jackson,testcontainers \
    --build=gradle \
    --lang=kotlin \
    --test=junit
If you don’t specify the --build argument, Gradle with the Kotlin DSL is used as the build tool.
If you don’t specify the --lang argument, Java is used as the language.
If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.

The previous command creates a Micronaut application with the default package example.micronaut in a directory named micronautguide.

If you use Micronaut Launch, select Micronaut Application as application type and add data-jdbc, liquibase, oracle, serialization-jackson, and testcontainers features.

If you have an existing Micronaut application and want to add the functionality described here, you can view the dependency and configuration changes from the specified features, and apply those changes to your application.

4.1. Micronaut Data JDBC

Add Micronaut Data JDBC dependencies to the project:

build.gradle
kapt("io.micronaut.data:micronaut-data-processor")
implementation("io.micronaut.data:micronaut-data-jdbc")
implementation("io.micronaut.sql:micronaut-jdbc-hikari")

4.2. Oracle Driver

Add also the Oracle Driver

build.gradle
runtimeOnly("com.oracle.database.jdbc:ojdbc11")

4.3. Data Source configuration

Define the datasource in src/main/resources/application.properties.

src/main/resources/application.properties
(1)
datasources.default.driver-class-name=oracle.jdbc.OracleDriver
(2)
datasources.default.db-type=oracle
(3)
datasources.default.dialect=ORACLE
1 Use the Oracle driver.
2 Set the database type.
3 Configure the Oracle dialect.

4.4. Database Migration with Liquibase

We need a way to create the database schema. For that, we use Micronaut integration with Liquibase.

Add the following snippet to include the necessary dependencies:

build.gradle
implementation("io.micronaut.liquibase:micronaut-liquibase")

Configure the database migrations directory for Liquibase in application.properties.

src/main/resources/application.properties
liquibase.datasources.default.change-log=classpath\:db/liquibase-changelog.xml

Create the following files with the database schema creation:

src/main/resources/db/liquibase-changelog.xml
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
  xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
  <include file="changelog/01-schema.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>
src/main/resources/db/changelog/01-schema.xml
<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
  xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
  <changeSet id="01" author="username" dbms="oracle">
    <createTable tableName="book">
      <column name="id" type="NUMBER(19,0)">
        <constraints primaryKey="true" primaryKeyName="pk_book" nullable="false"/>
      </column>
      <column name="title" type="VARCHAR2(255)">
        <constraints nullable="false"/>
      </column>
      <column name="pages" type="NUMBER(10,0)">
        <constraints nullable="true"/>
      </column>
    </createTable>
  </changeSet>
</databaseChangeLog>

The id column is the table’s primary key. Oracle’s MERGE statement matches rows with its ON condition, and the primary key on that same column keeps the upsert reliable under concurrent writes.

4.5. Entity

Create a Book entity:

src/main/kotlin/example/micronaut/Book.kt
package example.micronaut

import io.micronaut.data.annotation.Id
import io.micronaut.data.annotation.MappedEntity
import io.micronaut.serde.annotation.Serdeable

@Serdeable (1)
@MappedEntity (2)
data class Book(
    @field:Id val id: Long, (3)
    val title: String,
    val pages: Int? = null
)
1 Declare the @Serdeable annotation at the type level in your source code to allow the type to be serialized or deserialized.
2 Annotate the class with @MappedEntity to map the class to the table defined in the schema.
3 Specifies the ID of an entity
The id is not annotated with @GeneratedValue. By default, Micronaut Data uses the entity identity as the upsert conflict target, so the caller must supply the identity value. Upserts that rely on the default identity conflict target reject entities with generated identities.

4.6. Repository

Create a repository and declare an upsert method:

src/main/kotlin/example/micronaut/BookRepository.kt
package example.micronaut

import io.micronaut.data.jdbc.annotation.JdbcRepository
import io.micronaut.data.model.query.builder.sql.Dialect
import io.micronaut.data.repository.CrudRepository

@JdbcRepository(dialect = Dialect.ORACLE) (1)
interface BookRepository : CrudRepository<Book, Long> { (2)

    fun findByTitle(title: String): Book?

    fun upsert(book: Book) (3)
}
1 @JdbcRepository with a specific dialect.
2 By extending CrudRepository you enable automatic generation of CRUD (Create, Read, Update, Delete) operations.
3 Upsert methods are opt-in; they are not inherited from CrudRepository. Micronaut Data implements a method named exactly upsert (or upsertAll for an Iterable of entities) as a native upsert.

For the ORACLE dialect, Micronaut Data generates a MERGE statement at compilation time. For the Book entity, the generated SQL is similar to:

MERGE INTO "BOOK" target
USING (SELECT ? c0,? c1,? c2 FROM DUAL) source
ON (target."ID"=source.c0)
WHEN MATCHED THEN UPDATE SET target."TITLE"=source.c1,target."PAGES"=source.c2
WHEN NOT MATCHED THEN INSERT ("ID","TITLE","PAGES") VALUES (source.c0,source.c1,source.c2)

The bound values are selected from DUAL as a single-row source. If no row with the given id exists, the WHEN NOT MATCHED branch inserts a new row. Otherwise, the WHEN MATCHED branch updates the title and pages columns of the existing row.

Use the @Upsert annotation to give an upsert method a custom name, or its conflictsOn member to use another unique property, such as an ISBN, as the conflict target instead of the identity.

4.7. Controller

Create a controller which uses the upsert method both to create and to update a book:

src/main/kotlin/example/micronaut/BookController.kt
package example.micronaut

import io.micronaut.http.HttpStatus
import io.micronaut.http.annotation.Body
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Post
import io.micronaut.http.annotation.Put
import io.micronaut.http.annotation.Status

@Controller("/books") (1)
class BookController(private val bookRepository: BookRepository) { (2)

    @Post (3)
    @Status(HttpStatus.CREATED) (4)
    fun save(@Body book: Book) { (5)
        bookRepository.upsert(book) (6)
    }

    @Put (7)
    @Status(HttpStatus.OK)
    fun update(@Body book: Book) {
        bookRepository.upsert(book) (6)
    }
}
1 The class is defined as a controller with the @Controller annotation mapped to the path /books.
2 Use constructor injection to inject a bean of type BookRepository.
3 The @Post annotation maps the save method to an HTTP POST request on /books.
4 You can return void in your controller’s method and specify the HTTP status code via the @Status annotation.
5 The @Body annotation indicates that the request body should be parsed by Micronaut Framework and bound to the annotated parameter.
6 The same repository method inserts or updates the row, depending on whether a book with the supplied id already exists.
7 The @Put annotation maps the update method to an HTTP PUT request on /books.

4.8. Tests

Create an abstract test class that starts an Oracle Database container with Testcontainers and points the default datasource to it:

src/test/kotlin/example/micronaut/AbstractTest.kt
package example.micronaut

import io.micronaut.test.support.TestPropertyProvider
import org.testcontainers.oracle.OracleContainer
import org.testcontainers.utility.DockerImageName

abstract class AbstractTest : TestPropertyProvider { (1)

    companion object {
        const val IMAGE_NAME = "gvenzl/oracle-free:23.26.1-faststart"

        val container = OracleContainer(DockerImageName.parse(IMAGE_NAME)) (2)
    }

    override fun getProperties(): Map<String, String> {
        if (!container.isRunning) {
            container.start() (3)
        }
        return mapOf( (4)
            "datasources.default.driver-class-name" to "oracle.jdbc.OracleDriver",
            "datasources.default.url" to container.jdbcUrl,
            "datasources.default.username" to container.username,
            "datasources.default.password" to container.password
        )
    }
}
1 When you need dynamic properties definition, implement the TestPropertyProvider interface. Override the method .getProperties() and return the properties you want to expose to the application.
2 Declare the container once, so every test which extends the abstract class shares it.
3 Start the container if it is not running yet.
4 Use the Oracle JDBC driver and the container’s JDBC URL and credentials.

Create a test which sends a POST request to create a book and then a PUT request with the same id to update it:

src/test/kotlin/example/micronaut/BookControllerTest.kt
package example.micronaut

import io.micronaut.http.HttpRequest
import io.micronaut.http.HttpStatus
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance

@TestInstance(TestInstance.Lifecycle.PER_CLASS) (1)
@MicronautTest (2)
class BookControllerTest : AbstractTest() { (3)

    @Test
    fun bookUpsert(repository: BookRepository, (4)
                   @Client("/") httpClient: HttpClient) { (5)
        val client = httpClient.toBlocking()
        val path = "/books"
        val title = "Netty in Action"
        assertNull(repository.findByTitle(title)) (6)

        val postResponse = client.exchange<Map<String, Any>, Any>(
            HttpRequest.POST(path, mapOf("id" to 123L, "title" to title)))
        assertEquals(HttpStatus.CREATED, postResponse.status) (7)
        assertNotNull(repository.findByTitle(title))
        assertNull(repository.findByTitle(title)?.pages)

        val putResponse = client.exchange<Map<String, Any>, Any>(
            HttpRequest.PUT(path, mapOf("id" to 123L, "title" to title, "pages" to 265)))
        assertEquals(HttpStatus.OK, putResponse.status) (8)
        assertNotNull(repository.findByTitle(title)?.pages) (9)
    }
}
1 Classes that implement TestPropertyProvider must use this annotation to create a single class instance for all tests (not necessary in Spock tests).
2 Annotate the class with @MicronautTest so the Micronaut framework will initialize the application context and the embedded server. More info.
3 Extend AbstractTest to run the test against a database started by Testcontainers.
4 By default, with JUnit 5 the test method parameters will be resolved to beans if possible.
5 Inject the HttpClient bean and point it to the embedded server.
6 Initially, the book does not exist.
7 The first upsert inserts the book without pages.
8 The second upsert, with the same id, updates the existing row instead of failing with a duplicate key error.
9 The book now has pages.

5. Testing the Application

To run the tests:

./gradlew test

Then open build/reports/tests/test/index.html in a browser to see the results.

6. Native Tests

The io.micronaut.application Micronaut Gradle Plugin automatically integrates with GraalVM by applying the Gradle plugin for GraalVM Native Image building.

This plugin supports running tests on the JUnit Platform as native images. This means that tests will be compiled and executed as native code.

To execute the tests, execute:

./gradlew nativeTest

Then open build/reports/tests/test/index.html in a browser to see the results.

INFO: A test may be disabled within a GraalVM native image via the @DisabledInNativeImage annotation.

7. Next Steps

Explore more features with Micronaut Guides.

Read more about Upserts in the Micronaut Data documentation, including conflict targets, batch upserts, supported dialects, and lifecycle event behavior.

8. License

All guides are released with an Apache License 2.0 for the code and a Creative Commons Attribution 4.0 license for the writing and media (images).