private Crud<T, K> instantiateCrud(Connection connection) throws SQLException { if (table == null) { return crudDSL.to(connection); } else { return crudDSL.table(connection, table); } }
@Override public Crud<T, K> doInConnection(Connection connection) throws SQLException, DataAccessException { return factory.<T, K>crud(target, keyTarget).to(connection); } });
private Crud<T, K> instantiateCrud(Connection connection) throws SQLException { if (table == null) { return crudDSL.to(connection); } else { return crudDSL.table(connection, table); } }
/** * Create a connected crud validating it against the specified datasource. * The table name is derived from the jpa annotation or from the class name. * @param dataSource the datasource * @return a new crud instance * @throws SQLException if an error occurred */ public ConnectedCrud<T, K> to(DataSource dataSource) throws SQLException { Connection connection = dataSource.getConnection(); try { return new ConnectedCrud<T, K>(new DataSourceTransactionTemplate(dataSource), to(connection)); } finally { connection.close(); } }
/** * Create a connected crud validating it against the specified datasource. * The table name is derived from the jpa annotation or from the class name. * @param dataSource the datasource * @return a new crud instance * @throws SQLException if an error occurred */ public ConnectedCrud<T, K> to(DataSource dataSource) throws SQLException { Connection connection = dataSource.getConnection(); try { return new ConnectedCrud<T, K>(new DataSourceTransactionTemplate(dataSource), to(connection)); } finally { connection.close(); } }
@Test public void testDbObjectCrudTable() throws SQLException { Connection connection = DbHelper.getDbConnection(targetDB); if (connection == null) { System.err.println("Db " + targetDB + " not available"); return; } try { Crud<DbObjectTable, Long> objectCrud = JdbcMapperFactory.newInstance().<DbObjectTable, Long>crud(DbObjectTable.class, Long.class).to(connection); checkCrudDbObject(connection, objectCrud, DbObject.newInstance(new DbObjectTable())); } finally { connection.close(); } }
@Test public void testDbObjectCrudTestDbObject() throws SQLException { Connection connection = DbHelper.getDbConnection(targetDB); if (connection == null) { System.err.println("Db " + targetDB + " not available"); return; } try { Crud<TestDbObject, Long> objectCrud = JdbcMapperFactory.newInstance().<TestDbObject, Long>crud(TestDbObject.class, Long.class).to(connection); checkCrudDbObject(connection, objectCrud, DbObject.newInstance(new TestDbObject())); } finally { connection.close(); } }
@Test public void testDbObjectCrudTable() throws Exception { DataSource dataSource = DbHelper.getHsqlDataSource(); ConnectedCrud<CrudTest.DbObjectTable, Long> objectCrud = JdbcMapperFactory.newInstance().<CrudTest.DbObjectTable, Long>crud(CrudTest.DbObjectTable.class, Long.class).to(dataSource); checkCrudDbObject(objectCrud, DbObject.newInstance(new CrudTest.DbObjectTable())); Connection connection = dataSource.getConnection(); try { CrudTest.checkCrudDbObject(connection, objectCrud.crud(), DbObject.newInstance(new CrudTest.DbObjectTable())); } finally { connection.close(); } }
@Test public void testDbObjectCrudWithCustomGetter() throws Exception { JdbcMapperFactory mapperFactory = JdbcMapperFactory .newInstance() .addColumnProperty("name", new GetterProperty(new Getter<ResultSet, String>() { @Override public String get(ResultSet target) throws Exception { return "customname"; } })); Connection connection = DbHelper.getDbConnection(targetDB); if (connection == null) { System.err.println("Db " + targetDB + " not available"); return; } try { Crud<TestDbObject, Long> objectCrud = mapperFactory.<TestDbObject, Long>crud(TestDbObject.class, Long.class).to(connection); TestDbObject testDbObject = DbObject.newInstance(new TestDbObject()); objectCrud.create(connection, testDbObject); TestDbObject read = objectCrud.read(connection, testDbObject.getId()); assertEquals("customname", read.getName()); assertEquals(testDbObject.getEmail(), read.getEmail()); } finally { connection.close(); } }
@Test public void testDbObjectCrudWithCustomSetter() throws Exception { JdbcMapperFactory mapperFactory = JdbcMapperFactory.newInstance().addColumnProperty("name", new IndexedSetterProperty(new IndexedSetter<PreparedStatement, Object>() { @Override public void set(PreparedStatement target, Object value, int index) throws Exception { target.setString(index, "customname"); } })); Connection connection = DbHelper.getDbConnection(targetDB); if (connection == null) { System.err.println("Db " + targetDB + " not available"); return; } try { Crud<TestDbObject, Long> objectCrud = mapperFactory.<TestDbObject, Long>crud(TestDbObject.class, Long.class).to(connection); TestDbObject testDbObject = DbObject.newInstance(new TestDbObject()); assertNotEquals("customname", testDbObject); objectCrud.create(connection, testDbObject); TestDbObject read = objectCrud.read(connection, testDbObject.getId()); assertEquals("customname", read.getName()); assertEquals(testDbObject.getEmail(), read.getEmail()); } finally { connection.close(); } }