/* * HibernateFacade.java * * Created on April 25, 2005, 6:45 PM */ package book.hibernate; import book.business.Book; import book.business.BookFacade; import book.exceptions.DatabaseException; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; /** * * @author Sam Vaitheeswaran */ public class HibernateFacade implements BookFacade { private SessionFactory hibernateFactory; public book.business.Book getBook(String id) throws DatabaseException { Session session = null; Transaction tx = null; Book book = null; try { session = hibernateFactory.openSession(); tx = session.beginTransaction(); book = (Book)session.load(Book.class, id); tx.commit(); } catch (HibernateException he){ if(tx != null){ try { tx.rollback(); } catch(HibernateException he2){ throw new DatabaseException(he2); } } throw new DatabaseException(he); } finally { if(session != null){ try { session.close(); } catch(HibernateException he3){ throw new DatabaseException(he3); } } } return book; } public void deleteBook(String id) throws DatabaseException { // if id is null do not do any processing if(id == null){ return; } Session session = null; Transaction tx = null; try { session = hibernateFactory.openSession(); tx = session.beginTransaction(); Book book = (Book)session.load(Book.class, id); session.delete(book); tx.commit(); } catch (HibernateException he){ if(tx != null){ try { tx.rollback(); } catch(HibernateException he2){ throw new DatabaseException(he2); } } throw new DatabaseException(he); } finally { if(session != null){ try { session.close(); } catch(HibernateException he3){ throw new DatabaseException(he3); } } } } public void deleteBook(book.business.Book book) throws DatabaseException { // if book is null do not do any processing if(book == null){ return; } Session session = null; Transaction tx = null; try { session = hibernateFactory.openSession(); tx = session.beginTransaction(); session.delete(book); tx.commit(); } catch (HibernateException he){ if(tx != null){ try { tx.rollback(); } catch(HibernateException he2){ throw new DatabaseException(he2); } } throw new DatabaseException(he); } finally { if(session != null){ try { session.close(); } catch(HibernateException he3){ throw new DatabaseException(he3); } } } } public void updateBook(Book book) throws DatabaseException { Session session = null; Transaction tx = null; try { session = hibernateFactory.openSession(); tx = session.beginTransaction(); session.update(book); tx.commit(); } catch (HibernateException he){ if(tx != null){ try { tx.rollback(); } catch(HibernateException he2){ throw new DatabaseException(he2); } } throw new DatabaseException(he); } finally { if(session != null){ try { session.close(); } catch(HibernateException he3){ throw new DatabaseException(he3); } } } } public void saveBook(Book book) throws DatabaseException { Session session = null; Transaction tx = null; try { session = hibernateFactory.openSession(); tx = session.beginTransaction(); session.save(book); tx.commit(); } catch (HibernateException he){ if(tx != null){ try { tx.rollback(); } catch(HibernateException he2){ throw new DatabaseException(he2); } } throw new DatabaseException(he); } finally { if(session != null){ try { session.close(); } catch(HibernateException he3){ throw new DatabaseException(he3); } } } } public List listBooks() throws DatabaseException { Session session = null; Transaction tx = null; List result = null; try { session = hibernateFactory.openSession(); tx = session.beginTransaction(); Query q = session.createQuery("from Book as book"); result = q.list(); tx.commit(); } catch (HibernateException he){ if(tx != null){ try { tx.rollback(); } catch(HibernateException he2){ throw new DatabaseException(he2); } } throw new DatabaseException(he); } finally { if(session != null){ try { session.close(); } catch(HibernateException he3){ throw new DatabaseException(he3); } } } return result; } public HibernateFacade(SessionFactory factory) { hibernateFactory = factory; } public HibernateFacade() throws DatabaseException { try { hibernateFactory = new Configuration().configure().buildSessionFactory(); } catch (HibernateException he) { throw new DatabaseException(he); } } }