Search in sources :

Example 6 with MappingException

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();
    }
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) SfmResultSetMapperFactory(org.simpleflatmapper.jdbi.SfmResultSetMapperFactory) DBI(org.skife.jdbi.v2.DBI) MappingException(org.simpleflatmapper.map.MappingException) Handle(org.skife.jdbi.v2.Handle) MappingContext(org.simpleflatmapper.map.MappingContext) Mapper(org.simpleflatmapper.map.Mapper) ResultSet(java.sql.ResultSet) UnaryFactory(org.simpleflatmapper.util.UnaryFactory) Test(org.junit.Test)

Aggregations

MappingException (org.simpleflatmapper.map.MappingException)6 Test (org.junit.Test)5 ResultSet (java.sql.ResultSet)3 DbObject (org.simpleflatmapper.test.beans.DbObject)3 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 JdbcMapperFactory (org.simpleflatmapper.jdbc.JdbcMapperFactory)2 FieldMapperErrorHandler (org.simpleflatmapper.map.FieldMapperErrorHandler)2 Mapper (org.simpleflatmapper.map.Mapper)2 MappingContext (org.simpleflatmapper.map.MappingContext)2 SampleFieldKey (org.simpleflatmapper.test.map.SampleFieldKey)2 UnaryFactory (org.simpleflatmapper.util.UnaryFactory)2 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 Before (org.junit.Before)1 UncheckedConverter (org.simpleflatmapper.converter.UncheckedConverter)1 SfmResultSetMapperFactory (org.simpleflatmapper.jdbi.SfmResultSetMapperFactory)1 ConsumerErrorHandler (org.simpleflatmapper.map.ConsumerErrorHandler)1 FieldKey (org.simpleflatmapper.map.FieldKey)1 IgnoreMapperBuilderErrorHandler (org.simpleflatmapper.map.IgnoreMapperBuilderErrorHandler)1