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"));
});
}
Aggregations