...
A database object is a POJO (Plain Old Java Object) that is persisted in a single database table as a single row where each field of the POJO is mapped to a database column. It can be tedious to write JDBC CRUD (Create, Read, Update, Delete) operations without some kind of help. Therefore, we are using Spring's SimpleJdbcTemplate (see: Data access with JDBC ) for all CRUD operation on these database POJOs. Even with SimpleJdbcTemplate, there is a lot of boiler plate SQL is required , specially for createCRUD operations. To reduce the amount of work need needed to add new database objects we created a generic DAO (Data Access Object) to do most of the tedious CRUD operations called DBOBasicDao (W/implementation: DBOBasicDaoImpl).
...
Code Block |
---|
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:jdomodels-test-context.xml" }) public class DBONodeTypeTest { @Autowired DBOBasicDao dboBasicDao; private short id = 1000; @After public void after() throws DatastoreException{ if(dboBasicDao != null){ MapSqlParameterSource params = new MapSqlParameterSource(); params.addValue("id", id); dboBasicDao.deleteObjectById(DBONodeType.class, params); } } @Test public void testCRUD() throws Exception{ // Create a new type DBONodeType nodeType = new DBONodeType(); nodeType.setId(id); nodeType.setName("FakeType"); // Create it DBONodeType clone = dboBasicDao.createNew(nodeType); assertNotNull(clone); assertEquals(nodeType, clone); // Fetch it MapSqlParameterSource params = new MapSqlParameterSource(); params.addValue("id", id); clone = dboBasicDao.getObjectById(DBONodeType.class, params); assertNotNull(clone); assertEquals(nodeType.getId(), clone.getId()); assertEquals(nodeType.getName(), clone.getName()); // Delete it boolean result = dboBasicDao.deleteObjectById(DBONodeType.class, params); assertTrue("Failed to delete the type created", result); } } |