Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
public interface DatabaseObject<T> {
	
	/**
	 * Get the database Mapping for this object.
	 * @return
	 */
	public TableMapping<T> getTableMapping();

}

...

Code Block
public interface TableMapping<T> extends RowMapper<T> {
	
	/**
	 * The name of the database table.
	 * @return
	 */
	public String getTableName();
	/**
	 * The name of the DDL file that defines this table.
	 * @return
	 */
	public String getDDLFileName();
	
	/**
	 * Maps field names to column names.
	 * @return
	 */
	public FieldColumn[] getFieldColumns();
	
	/**
	 * The class for <T>
	 * @return
	 */
	public Class<? extends T> getDBOClass();

}

...

Code Block
public class DBONodeType {
	
	private Short id;
	private String name;
}

...

Code Block
public class DBONodeType implements DatabaseObject<DBONodeType> {
	

	private static FieldColumn[] FIELDS = new FieldColumn[]{
			new FieldColumn("id", "ID", true),
			new FieldColumn("name", "NAME"),
	};
	
	@Override
	public TableMapping<DBONodeType> getTableMapping() {
		return new TableMapping<DBONodeType>(){
			// Map a result set to this object
			@Override
			public DBONodeType mapRow(ResultSet rs, int rowNum)	throws SQLException {
				DBONodeType result = new DBONodeType();
				result.setId(rs.getShort("ID"));
				result.setName(rs.getString("NAME"));
				return result;
			}

			@Override
			public String getTableName() {
				return "NODE_TYPE";
			}

			@Override
			public String getDDLFileName() {
				return "schema/NodeType-ddl.sql";
			}

			@Override
			public FieldColumn[] getFieldColumns() {
				return FIELDS;
			}

			@Override
			public Class<? extends DBONodeType> getDBOClass() {
				return DBONodeType.class;
			}} ;
	}
	
	private Short id;
	private String name;

	public Short getId() {
		return id;
	}

	public void setId(Short id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

...

Once we have defined our database POJO the last thing we need to do is register the DBONodeType class with the DBOBasicDao . Registration is necessary because the DBOBasicDao must create any table that does not exist and it needs to auto-generate all CRUD SQL used for instances of our new class DBONodeType. We register the DBONodeType class by adding an instance of it to the DBOBasicDaoImpl bean file 'dbo-beans.spb.xml':

Code Block
	<bean id="dboBasicDao" class="org.sagebionetworks.repo.model.dbo.DBOBasicDaoImpl">
		<property name="databaseObjectRegister">
			<list>
				<bean class="org.sagebionetworks.repo.model.dbo.persistence.DBONodeType" />
				....
			</list>
		</property>
	</bean>

The We are now ready to start testing our new database object:

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);
		
	}

}