Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.facade; import com.utils.MyUtils; import java.io.Serializable; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import org.hibernate.Criteria; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.criterion.Projections; public abstract class AbstractFacade<T> implements Serializable { public final Class<T> entityClass; public AbstractFacade(Class<T> entityClass) { this.entityClass = entityClass; } public void createTransactionSession(Transaction trans, Session session) throws HibernateException { } public T create(T entity) throws Exception { Transaction trans = null; Session session = null; try { session = MyUtils.getSession(); trans = session.beginTransaction(); if (entity == null) { throw new NullPointerException(); } session.save(entity); trans.commit(); } catch (HibernateException e) { try { if (trans != null && trans.isActive()) { trans.rollback(); } } catch (Exception ex) { throw ex; } Logger.getLogger(entityClass.getName()).log(Level.SEVERE, null, e); throw new Exception(e); } finally { MyUtils.closeSession(session); } return entity; } public void edit(T entity) throws Exception { Transaction trans = null; Session session = null; try { session = MyUtils.getSession(); trans = session.beginTransaction(); session.update(entity); trans.commit(); } catch (Exception e) { try { if (trans != null && trans.isActive()) { trans.rollback(); } } catch (Exception ex) { throw ex; } Logger.getLogger(entityClass.getName()).log(Level.SEVERE, null, e); throw new Exception(e); } finally { MyUtils.closeSession(session); } } public void remove(T entity) throws Exception { Transaction trans = null; Session session = null; try { session = MyUtils.getSession(); trans = session.beginTransaction(); session.delete(entity); trans.commit(); } catch (Exception e) { try { if (trans != null && trans.isActive()) { trans.rollback(); } } catch (Exception ex) { throw ex; } Logger.getLogger(entityClass.getName()).log(Level.SEVERE, null, e); throw new Exception(e); } finally { MyUtils.closeSession(session); } } public T find(Object id) throws Exception { Transaction trans = null; Session session = null; try { session = MyUtils.getSession(); trans = session.beginTransaction(); T obj = (T) session.get(entityClass, (Serializable) id); trans.commit(); return obj; } catch (Exception e) { try { if (trans != null && trans.isActive()) { trans.rollback(); } } catch (Exception ex) { throw ex; } Logger.getLogger(entityClass.getName()).log(Level.SEVERE, null, e); throw new Exception(e); } finally { MyUtils.closeSession(session); } } public List<T> findAll() { Transaction trans = null; Session session = null; List<T> list = null; try { session = MyUtils.getSession(); if (session != null) { trans = session.beginTransaction(); list = session.createCriteria(entityClass).list(); trans.commit(); } } catch (Exception e) { try { if (trans != null && trans.isActive()) { trans.rollback(); } } catch (Exception ex) { throw ex; } Logger.getLogger(entityClass.getName()).log(Level.SEVERE, null, e); } finally { MyUtils.closeSession(session); } return list; } public int count() { Transaction trans = null; Session session = null; int cnt = 0; try { session = MyUtils.getSession(); trans = session.beginTransaction(); Criteria cq = session.createCriteria(entityClass); cq.setProjection(Projections.rowCount()); cnt = ((Long) cq.uniqueResult()).intValue(); trans.commit(); } catch (Exception e) { try { if (trans != null && trans.isActive()) { trans.rollback(); } } catch (Exception ex) { throw ex; } Logger.getLogger(entityClass.getName()).log(Level.SEVERE, null, e); } finally { MyUtils.closeSession(session); } return cnt; } }