Manager Classes: Spring-Hibernate Integration


In Chapter 10, "Beyond the Basics," we discussed the Spring and Hibernate integration. By leveraging and using Spring's declarative transaction management support, we were able to reduce the lines of code in our Manager classes significantly. The refactored code can be found under the rapidjava/timex2/ directory.

For example, the following is the original method in our TimesheetManager class (found in timex/) to save a Timesheet object:

public void saveTimesheet(Timesheet timesheet) {     Session session = HibernateUtil.getSessionFactory()             .getCurrentSession();     session.beginTransaction();     try     {         session.saveOrUpdate(timesheet);         session.getTransaction().commit();     }     catch (HibernateException e)     {         session.getTransaction().rollback();         throw e;     } }


This code was reduced to just one line! The new saveTimesheet method can be found under timex2/ and looks like this:

public void saveTimesheet(Timesheet timesheet) {     getHibernateTemplate().merge(timesheet); }




Agile Java Development with Spring, Hibernate and Eclipse
Agile Java Development with Spring, Hibernate and Eclipse
ISBN: 0672328968
EAN: 2147483647
Year: 2006
Pages: 219

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net