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
    • Follow these steps to implement an existing interface class in Eclipse
    1. Choose or create a folder in a java source directory to put the implementation class in
    2. Right click on that folder and select New -> Class
    3. Enter a Name for the implementation class (e.g. MyClassImpl)
    4. Click the Add button to the right of Interfaces:
    5. Enter the name of the existing interface class (all near matches should appear in the box below)
    6. Select the interface class in the Matching Types: box and click OK
    7. Under Which method stubs would you like to create? check the box marked Inherited abstract methods
      • Note: All other checkboxes should be unchecked
    8. Click Finish to generate the new interface implementation class
  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
    1. Adding the commons-logger dependency to your project.xml
      1. Add the following to the dependencies block of your project.xml file to include the commons-logger:
        <dependency>
        	<groupId>commons-logging</groupId>
        	<artifactId>commons-logging</artifactId>
        	<version>1.0.4</version>
        </dependency>
        
        • The commons logger is included in shared/lib by Sakai itself so you do not need to package it in your tool war file (hence there is no <war.bundle>true</war.bundle>)
        • Version 1.0.4 was the current version when this was written
    2. Adding the logger to your code
      1. Add the following line just below your class line:
        private static Log log = LogFactory.getLog(MyClass.class);
        
        • Make sure your class name is listed in place of MyClass
      2. The commons logger requires 2 imports (eclipse will insert these for you):
        import org.apache.commons.logging.Log;
        import org.apache.commons.logging.LogFactory;
        
  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
      • This shows how to use Eclipse to insert a try-catch block around a statement which automatically includes possible exceptions
      1. Select the statement to surround with a try-catch block
      2. Right click on the selection and choose Source -> Surround with Try / Catch Block
      3. You should now edit the Auto-generated catch block(s)
        • Note: don't forget to remove the e.printStackTrace(); if you do not want this exception to put a stackTrace in the logs
    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
      • This shows how to use Eclipse to insert a try-catch block around a statement which automatically includes possible exceptions
      1. Select the statement to surround with a try-catch block
      2. Right click on the selection and choose Source -> Surround with Try / Catch Block
      3. You should now edit the Auto-generated catch block(s)
        • Note: don't forget to remove the e.printStackTrace(); if you do not want this exception to put a stackTrace in the logs
    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