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.java- Follow these steps to implement an existing interface class in Eclipse
- Choose or create a folder in a java source directory to put the implementation class in
- Right click on that folder and select New -> Class
- Enter a Name for the implementation class (e.g. MyClassImpl)
- Click the Add button to the right of Interfaces:
- Enter the name of the existing interface class (all near matches should appear in the box below)
- Select the interface class in the Matching Types: box and click OK
- Under Which method stubs would you like to create? check the box marked Inherited abstract methods
- Note: All other checkboxes should be unchecked
- Click Finish to generate the new interface implementation class
- Extending HibernateDaoSupport
Using this method gives us easy access to a Spring HibernateTemplate, Hibernate SessionFactory, and Hibernate Session- Edit the class line in TaskListManagerDaoImpl from:
to:
public class TaskListManagerDaoImpl implements TaskListManager {
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
- Edit the class line in TaskListManagerDaoImpl from:
- Adding the commons logger
- Using the Apache commons logger is the recommended way to do logging in Sakai tools
- Adding the commons-logger dependency to your project.xml
- 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
- Add the following to the dependencies block of your project.xml file to include the commons-logger:
- Adding the logger to your code
- 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
- The commons logger requires 2 imports (eclipse will insert these for you):
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory;
- Add the following line just below your class line:
- 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
- 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
- This shows how to use Eclipse to insert a try-catch block around a statement which automatically includes possible exceptions
- Select the statement to surround with a try-catch block
- Right click on the selection and choose Source -> Surround with Try / Catch Block
- 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
- 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 failed- Use 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
- This shows how to use Eclipse to insert a try-catch block around a statement which automatically includes possible exceptions
- Select the statement to surround with a try-catch block
- Right click on the selection and choose Source -> Surround with Try / Catch Block
- 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
- 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 statement- Insert 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 ";"
- Insert the required imports for our use of criterion
- 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