Search in sources :

Example 1 with ResultBearing

use of org.jdbi.v3.core.result.ResultBearing in project tutorials by eugenp.

the class JdbiTest method whenIdentityColumn_thenInsertReturnsNewId.

@Test
public void whenIdentityColumn_thenInsertReturnsNewId() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    jdbi.useHandle(handle -> {
        handle.execute("create table PROJECT_2 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
        Update update = handle.createUpdate("INSERT INTO PROJECT_2 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')");
        ResultBearing generatedKeys = update.executeAndReturnGeneratedKeys();
        assertEquals(0, generatedKeys.mapToMap().findOnly().get("id"));
        update = handle.createUpdate("INSERT INTO PROJECT_2 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')");
        assertEquals(1, generatedKeys.mapToMap().findOnly().get("id"));
    });
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) ResultBearing(org.jdbi.v3.core.result.ResultBearing) Update(org.jdbi.v3.core.statement.Update) Test(org.junit.Test)

Aggregations

Jdbi (org.jdbi.v3.core.Jdbi)1 ResultBearing (org.jdbi.v3.core.result.ResultBearing)1 Update (org.jdbi.v3.core.statement.Update)1 Test (org.junit.Test)1