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.js.jd.models.dao.hql; import com.js.jd.exception.BussinessException; import com.js.jd.exception.BussinessMessage; import com.js.jd.models.dao.AbstractTemplateDao; import com.js.jd.models.dao.GenericDao; import java.io.Serializable; import java.lang.reflect.ParameterizedType; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; /** * * @author Fred Pea * @param <T> * @param <ID> */ public class HQLGenericDAO<T, ID extends Serializable> extends AbstractTemplateDao<T, ID> implements GenericDao<T, ID> { private final SessionFactory sessionFactory; private static final Logger LOGGER = Logger.getLogger(HQLGenericDAO.class.getName()); public HQLGenericDAO(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @Override public void saveOrUpdate(T entity) throws BussinessException { Session session = sessionFactory.getCurrentSession(); try { session.beginTransaction(); session.saveOrUpdate(entity); session.getTransaction().commit(); } catch (javax.validation.ConstraintViolationException | org.hibernate.exception.ConstraintViolationException cve) { try { if (session.getTransaction().isActive()) { session.getTransaction().rollback(); } } catch (Exception exc) { LOGGER.log(Level.WARNING, "Fall al hacer un rollback", exc); } throw new BussinessException(cve); } catch (RuntimeException ex) { try { if (session.getTransaction().isActive()) { session.getTransaction().rollback(); } } catch (Exception exc) { LOGGER.log(Level.WARNING, "Fall al hacer un rollback", exc); } throw ex; } catch (Exception ex) { try { if (session.getTransaction().isActive()) { session.getTransaction().rollback(); } } catch (Exception exc) { LOGGER.log(Level.WARNING, "Fall al hacer un rollback", exc); } throw new RuntimeException(ex); } } @Override public T read(ID id) throws BussinessException { Session session = sessionFactory.getCurrentSession(); try { session.beginTransaction(); T entity = (T) session.get(getEntityClass(), id); session.getTransaction().commit(); return entity; } catch (javax.validation.ConstraintViolationException | org.hibernate.exception.ConstraintViolationException cve) { try { if (session.getTransaction().isActive()) { session.getTransaction().rollback(); } } catch (Exception exc) { LOGGER.log(Level.WARNING, "Fall al hacer un rollback", exc); } throw new BussinessException(cve); } catch (RuntimeException ex) { try { if (session.getTransaction().isActive()) { session.getTransaction().rollback(); } } catch (Exception exc) { LOGGER.log(Level.WARNING, "Fall al hacer un rollback", exc); } throw ex; } catch (Exception ex) { try { if (session.getTransaction().isActive()) { session.getTransaction().rollback(); } } catch (Exception exc) { LOGGER.log(Level.WARNING, "Fall al hacer un rollback", exc); } throw new RuntimeException(ex); } } @Override public void remove(ID id) throws BussinessException { Session session = sessionFactory.getCurrentSession(); try { session.beginTransaction(); T entity = (T) session.get(getEntityClass(), id); if (entity == null) { throw new BussinessException(new BussinessMessage(null, "Los datos a borrar ya no existen")); } session.delete(entity); session.getTransaction().commit(); } catch (javax.validation.ConstraintViolationException | org.hibernate.exception.ConstraintViolationException cve) { try { if (session.getTransaction().isActive()) { session.getTransaction().rollback(); } } catch (Exception exc) { LOGGER.log(Level.WARNING, "Fall al hacer un rollback", exc); } throw new BussinessException(cve); } catch (BussinessException | RuntimeException ex) { try { if (session.getTransaction().isActive()) { session.getTransaction().rollback(); } } catch (Exception exc) { LOGGER.log(Level.WARNING, "Fall al hacer un rollback", exc); } throw ex; } } @Override public List<T> findAll() throws BussinessException { Session session = sessionFactory.getCurrentSession(); try { session.beginTransaction(); Query query = session.createQuery("SELECT e FROM " + getEntityClass().getName() + " e"); List<T> entities = query.list(); session.getTransaction().commit(); return entities; } catch (javax.validation.ConstraintViolationException | org.hibernate.exception.ConstraintViolationException cve) { try { if (session.getTransaction().isActive()) { session.getTransaction().rollback(); } } catch (Exception exc) { LOGGER.log(Level.WARNING, "Fall al hacer un rollback", exc); } throw new BussinessException(cve); } catch (HibernateException ex) { try { if (session.getTransaction().isActive()) { session.getTransaction().rollback(); } } catch (Exception exc) { LOGGER.log(Level.WARNING, "Fall al hacer un rollback", exc); } throw ex; } } private Class<T> getEntityClass() { return (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; } }