Look-up Tables: DAO components
6 July 2008Now that we have the tests set up for the look-up table data access object, it’s time to create the actual data access object. In continuing my approach of modeling my components off of those found in the AppFuse Light collection, I used the UserDaoHibernate module as the basis of my own LookupTableDaoHibernate class. Of course, in AppFuse Light, UserDaoHibernate implements interface UserDao which extends the Dao interface, so I started at the beginning:
package org.restafarian.core.dao;
/**
* Data Access Object (Dao) interface. This is an empty interface
* used to tag our Dao classes. Common methods for each interface
* could be added here.
*/
public interface Dao {
}
There’s virtually no difference between this one and the original, but I strayed just a little bit off of the pattern on the LookupTableDao interface, because I wanted to add a couple of additional ways to get to the data:
package org.restafarian.core.dao;
import java.util.List;
import org.restafarian.core.beans.LookupTable;
/**
* <p>This is the LookupTable data access object interface.</p>
*/
public interface LookupTableDao extends Dao {
/**
* <p>Returns all LookupTables in the database.</p>
*
* @return all LookupTables in the database
*/
public List findAll();
/**
* <p>Returns all LookupTables in the database that
* match the specified search criteria.</p>
*
* @param lookupTable an example LookupTable
* @return all LookupTables in the database that
* match the specified search criteria
*/
public List findByExample(LookupTable lookupTable);
/**
* <p>Returns all LookupTables in the database that
* match the specified search criteria.</p>
*
* @param propertyName the name of the specified property
* @param propertyValue the search value for the specified
* property
* @return all LookupTables in the database that
* match the specified search criteria
*/
public List findByProperty(String propertyName, Object propertyValue);
/**
* <p>Returns the LookupTable with the specified id.</p>
*
* @param id the id of the requested lookupTable
* @return the LookupTable with the specified id
*/
public LookupTable findById(String LookupTableId);
/**
* <p>Saves the LookupTable passed.</p>
*
* @param lookupTable the lookupTable to save
*/
public void save(LookupTable LookupTable);
/**
* <p>Deletes the LookupTable with the specified id.</p>
*
* @param id the id of the lookupTable to delete
*/
public void delete(String LookupTableId);
}
That just leaves the Hibernate implementation, which again, pretty much follows the original with the addition of the extra data access methods:
package org.restafarian.core.dao.hibernate;
import java.util.List;
import org.restafarian.core.beans.LookupTable;
import org.restafarian.core.dao.LookupTableDao;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
* <p>This is the LookupTable data access object.</p>
*/
public class LookupTableDaoHibernate extends HibernateDaoSupport implements LookupTableDao {
/**
* <p>Returns all LookupTables in the database.</p>
*
* @return all LookupTables in the database
*/
public List findAll() {
return getHibernateTemplate().find("from LookupTable order by id");
}
/**
* <p>Returns all LookupTables in the database that
* match the specified search criteria.</p>
*
* @param lookupTable an example LookupTable
* @return all LookupTables in the database that
* match the specified search criteria
*/
public List findByExample(LookupTable lookupTable) {
return getHibernateTemplate().findByExample(lookupTable);
}
/**
* <p>Returns all LookupTables in the database that
* match the specified search criteria.</p>
*
* @param propertyName the name of the specified property
* @param propertyValue the search value for the specified
* property
* @return all LookupTables in the database that
* match the specified search criteria
*/
public List findByProperty(String propertyName, Object propertyValue) {
StringBuffer buffer = new StringBuffer();
buffer.append("from LookupTable where ");
buffer.append(propertyName);
buffer.append(" = '");
buffer.append(propertyValue);
buffer.append("' order by id");
return getHibernateTemplate().find(buffer.toString());
}
/**
* <p>Returns the LookupTable with the specified id.</p>
*
* @param id the id of the requested lookupTable
* @return the LookupTable with the specified id
*/
public LookupTable findById(String id) {
LookupTable lookupTable = (LookupTable) getHibernateTemplate().get(LookupTable.class, id);
if (lookupTable == null) {
throw new ObjectRetrievalFailureException(LookupTable.class, id);
}
return lookupTable;
}
/**
* <p>Saves the LookupTable passed.</p>
*
* @param lookupTable the lookupTable to save
*/
public void save(LookupTable lookupTable) {
getHibernateTemplate().saveOrUpdate(lookupTable);
}
/**
* <p>Deletes the LookupTable with the specified id.</p>
*
* @param id the id of the lookupTable to delete
*/
public void delete(String id) {
getHibernateTemplate().delete(findById(id));
}
}
That pretty much completes the Dao and all of its related parts, but before we can actually pull the trigger on the tests, we still have a little work to do in order to configure this all up via Spring. That will be the subject of our next installment.
Sorry, the comment form is closed at this time.





