use of org.simpleflatmapper.map.MappingException in project SimpleFlatMapper by arnaudroger.
the class ResultSetMapperFactoryTest method testMapToDbObjectStatic.
@Test
public void testMapToDbObjectStatic() throws Exception {
DBI dbi = new DBI(DbHelper.getHsqlDataSource());
dbi.registerMapper(new SfmResultSetMapperFactory(new UnaryFactory<Class<?>, Mapper<ResultSet, ?>>() {
@Override
public Mapper<ResultSet, ?> newInstance(Class<?> aClass) {
return new Mapper<ResultSet, DbObject>() {
@Override
public DbObject map(ResultSet source) throws MappingException {
return map(source, null);
}
@Override
public DbObject map(ResultSet source, MappingContext<? super ResultSet> context) throws MappingException {
DbObject dbObject = new DbObject();
try {
mapTo(source, dbObject, context);
} catch (Exception e) {
e.printStackTrace();
}
return dbObject;
}
@Override
public void mapTo(ResultSet source, DbObject target, MappingContext<? super ResultSet> context) throws Exception {
target.setId(source.getInt("id"));
target.setCreationTime(source.getTimestamp("creation_time"));
target.setEmail(source.getString("email"));
target.setName(source.getString("name"));
String type_name = source.getString("type_name");
if (type_name != null) {
target.setTypeName(DbObject.Type.valueOf(type_name));
}
target.setTypeOrdinal(DbObject.Type.values()[source.getInt("type_ordinal")]);
}
};
}
}));
Handle handle = dbi.open();
try {
DbObject dbObject = handle.createQuery(DbHelper.TEST_DB_OBJECT_QUERY).mapTo(DbObject.class).first();
DbHelper.assertDbObjectMapping(dbObject);
SfmBindTest.SfmBindExample attach = handle.attach(SfmBindTest.SfmBindExample.class);
attach.insert(DbObject.newInstance());
assertTrue(handle.createQuery("select * from TEST_DB_OBJECT").mapTo(DbObject.class).list().size() > 1);
} finally {
handle.close();
}
}
Aggregations