use of org.simpleflatmapper.util.CheckedConsumer in project SimpleFlatMapper by arnaudroger.
the class JdbcTemplateCrudTest method testCrud.
@Test
public void testCrud() throws SQLException {
JdbcTemplateCrud<DbObject, Long> objectCrud = JdbcTemplateMapperFactory.newInstance().<DbObject, Long>crud(DbObject.class, Long.class).to(template, "TEST_DB_OBJECT");
DbObject object = DbObject.newInstance();
assertNull(objectCrud.read(object.getId()));
// create
Long key = objectCrud.create(object, new CheckedConsumer<Long>() {
Long key;
@Override
public void accept(Long aLong) throws Exception {
key = aLong;
}
}).key;
assertNull(key);
key = object.getId();
// read
assertEquals(object, objectCrud.read(key));
object.setName("Udpdated");
// update
objectCrud.update(object);
assertEquals(object, objectCrud.read(key));
// delete
objectCrud.delete(key);
assertNull(objectCrud.read(key));
objectCrud.create(object);
assertEquals(object, objectCrud.read(key));
objectCrud.delete(key);
try {
objectCrud.createOrUpdate(object);
assertEquals(object, objectCrud.read(key));
} catch (UnsupportedOperationException e) {
}
}
use of org.simpleflatmapper.util.CheckedConsumer in project SimpleFlatMapper by arnaudroger.
the class PostgresqlCrudTest method testSerial.
@Test
public void testSerial() throws SQLException {
Connection connection = DbHelper.getDbConnection(DbHelper.TargetDB.POSTGRESQL);
if (connection == null) {
System.err.println("Db POSTGRESQL not available");
return;
}
try {
Statement st = connection.createStatement();
try {
st.execute("CREATE TABLE IF NOT EXISTS test_db_object_serial( id bigserial primary key," + " name varchar(100), " + " email varchar(100)," + " creation_Time timestamp, type_ordinal int, type_name varchar(10) )");
} finally {
st.close();
}
Crud<DbObject, Long> objectCrud = JdbcMapperFactory.newInstance().addKeys("id").<DbObject, Long>crud(DbObject.class, Long.class).table(connection, "test_db_object_serial");
DbObject object = DbObject.newInstance();
object.setId(-22225);
// create
Long key = objectCrud.create(connection, object, new CheckedConsumer<Long>() {
Long key;
@Override
public void accept(Long aLong) throws Exception {
key = aLong;
}
}).key;
assertFalse(key.equals(object.getId()));
object.setId(key);
// read
assertEquals(object, objectCrud.read(connection, key));
} finally {
connection.close();
}
}
Aggregations