Tasklist manager hibernate implementation

Tasklist manager hibernate implementation

  • These steps outline the creation of an implementation of the tasklist manager api for hibernate

  1. Creating the API implementation
    We will be implementing the TaskListManager.java interface using a class called TaskListManagerDaoImpl.java

  2. Extending HibernateDaoSupport
    Using this method gives us easy access to a Spring HibernateTemplate, Hibernate SessionFactory, and Hibernate Session

    1. Edit the class line in TaskListManagerDaoImpl from:

      public class TaskListManagerDaoImpl implements TaskListManager {

      to:

      public class TaskListManagerDaoImpl extends HibernateDaoSupport implements TaskListManager {
    2. 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

    3. You should now have a class implementation with 3 function stubs

  3. Adding the commons logger

  4. 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 failed

    • Use the HibernateTemplate to access the save method

    1. Enter getHibernateTemplate() in place of the TODO comment line

    2. Enter a "." to see the list of available methods in Eclipse

    3. Choose the save method with a single object argument

    4. Enter the name of the Task object (should be t) as the argument

    5. Close the statement with a semicolon ";"

    6. Surround the statement with a try-catch block

    7. Replace the TODO comment line with a logging statement like this one:

      log.error("Hibernate could not save: " + e.toString());
    8. Add return false; after the e.printStackTrace(); line

    9. 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; }
  5. 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 failed

    • Use the HibernateTemplate to access the delete method

    1. Enter getHibernateTemplate() in place of the TODO comment line

    2. Enter a "." to see the list of available methods in Eclipse

    3. Choose the delete method with a single object argument

    4. Enter the name of the Task object (should be t) as the argument

    5. Close the statement with a semicolon ";"

    6. Surround the statement with a try-catch block

    7. Replace the TODO comment line with a logging statement like this one:

      log.error("Hibernate could not delete: " + e.toString());
    8. Add return false; after the e.printStackTrace(); line

    9. 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; }
  6. 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

    1. Enter getHibernateTemplate() in place of the auto-generate return value:

      return getHibernateTemplate()
    2. Enter a "." to see the list of available methods in Eclipse

    3. Choose the findByCriteria method with a single object argument

    4. Enter the name of the Criteria object we will create (should be d) as the argument

    5. Close the statement with a semicolon ";"

    6. 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 statement

      1. Insert the required imports for our use of criterion

        import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.Order; import org.hibernate.criterion.Restrictions;
      2. Enter DetachedCriteria d = DetachedCriteria in place of the TODO comment line

      3. Enter a "." to see the list of available DetachedCriteria methods in Eclipse

      4. Choose the forClass method with a single object argument

      5. Enter the Task object class (Task.class) as the argument

      6. Enter a "." to see the list of available DetachedCriteria methods in Eclipse

      7. Choose the add method with a single object argument

      8. Enter "Restrictions" as the argument

      9. Enter a "." to see the list of available Restrictions methods in Eclipse

      10. Choose the eq method

      11. Enter the string name of the siteId variable in the Task object (siteId) for argument 1

      12. Enter the name of the passed siteId object (siteId) for argument 2

      13. Enter a "." to see the list of available DetachedCriteria methods in Eclipse

      14. Choose the addOrder method with a single object argument

      15. Enter "Order" as the argument

      16. Enter a "." to see the list of available Order methods in Eclipse

      17. Choose the desc method

      18. Enter the string name of the creationDate variable in the Task object (creationDate) for the argument

      19. 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); }
  1. Class implementation complete