Tasklist manager hibernate implementation
These steps outline the creation of an implementation of the tasklist manager api for hibernate
Creating the API implementation
We will be implementing the TaskListManager.java interface using a class called TaskListManagerDaoImpl.javaExtending HibernateDaoSupport
Using this method gives us easy access to a Spring HibernateTemplate, Hibernate SessionFactory, and Hibernate SessionEdit the class line in TaskListManagerDaoImpl from:
public class TaskListManagerDaoImpl implements TaskListManager {to:
public class TaskListManagerDaoImpl extends HibernateDaoSupport implements TaskListManager {Add the following import manually or use Eclipse to do it for you:
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;Note: Make sure you are using the hibernate3 version
You should now have a class implementation with 3 function stubs
Adding the commons logger
Implementing saveTask
This function is meant to insert an object into persistent storage and return a boolean value which indicates if the insertion suceeded or failedUse the HibernateTemplate to access the save method
Enter getHibernateTemplate() in place of the TODO comment line
Enter a "." to see the list of available methods in Eclipse
Choose the save method with a single object argument
Enter the name of the Task object (should be t) as the argument
Close the statement with a semicolon ";"
Surround the statement with a try-catch block
Replace the TODO comment line with a logging statement like this one:
log.error("Hibernate could not save: " + e.toString());Add return false; after the e.printStackTrace(); line
Change the auto-generated return false; line at the end of the function with return true;
Here is the complete function:
public boolean saveTask(Task t) { try { getHibernateTemplate().save(t); } catch (DataAccessException e) { log.error("Hibernate could not save: " + e.toString()); e.printStackTrace(); return false; } return true; }
Implementing deleteTask
This function is meant to remove an object from persistent storage and return a boolean value which indicates if the insertion suceeded or failedUse the HibernateTemplate to access the delete method
Enter getHibernateTemplate() in place of the TODO comment line
Enter a "." to see the list of available methods in Eclipse
Choose the delete method with a single object argument
Enter the name of the Task object (should be t) as the argument
Close the statement with a semicolon ";"
Surround the statement with a try-catch block
Replace the TODO comment line with a logging statement like this one:
log.error("Hibernate could not delete: " + e.toString());Add return false; after the e.printStackTrace(); line
Change the auto-generated return false; line at the end of the function with return true;
Here is the complete function:
public boolean deleteTask(Task t) { try { getHibernateTemplate().delete(t); } catch (DataAccessException e) { log.error("Hibernate could not delete: " + e.toString()); e.printStackTrace(); return false; } return true; }
Implementing findAllTasks
This function is meant to retrieve a collection of all objects from persistent storage based on the passed siteId (or Context)Use the HibernateTemplate to access the findByCriteria method
Enter getHibernateTemplate() in place of the auto-generate return value:
return getHibernateTemplate()Enter a "." to see the list of available methods in Eclipse
Choose the findByCriteria method with a single object argument
Enter the name of the Criteria object we will create (should be d) as the argument
Close the statement with a semicolon ";"
Create the DetachedCriteria object (d) to do our find
This allows us to restrict which objects are returned and sort them, it is sorta like the "where" and "order by" in an SQL statementInsert the required imports for our use of criterion
import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.Order; import org.hibernate.criterion.Restrictions;Enter DetachedCriteria d = DetachedCriteria in place of the TODO comment line
Enter a "." to see the list of available DetachedCriteria methods in Eclipse
Choose the forClass method with a single object argument
Enter the Task object class (Task.class) as the argument
Enter a "." to see the list of available DetachedCriteria methods in Eclipse
Choose the add method with a single object argument
Enter "Restrictions" as the argument
Enter a "." to see the list of available Restrictions methods in Eclipse
Choose the eq method
Enter the string name of the siteId variable in the Task object (siteId) for argument 1
Enter the name of the passed siteId object (siteId) for argument 2
Enter a "." to see the list of available DetachedCriteria methods in Eclipse
Choose the addOrder method with a single object argument
Enter "Order" as the argument
Enter a "." to see the list of available Order methods in Eclipse
Choose the desc method
Enter the string name of the creationDate variable in the Task object (creationDate) for the argument
Close the statement with a semicolon ";"
Here is the complete function:
public Collection findAllTasks(String siteId) { DetachedCriteria d = DetachedCriteria.forClass(Task.class) .add( Restrictions.eq("siteId", siteId) ) .addOrder( Order.desc("creationDate") ); return getHibernateTemplate().findByCriteria(d); }
Class implementation complete
The code for this entire class is available in the cafe SVN - TaskListManagerDaoImpl