diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/common/daos/impl/GenericDaoHibernateTemplate.java b/navalplanner-business/src/main/java/org/navalplanner/business/common/daos/impl/GenericDaoHibernateTemplate.java deleted file mode 100644 index c12176d7c..000000000 --- a/navalplanner-business/src/main/java/org/navalplanner/business/common/daos/impl/GenericDaoHibernateTemplate.java +++ /dev/null @@ -1,161 +0,0 @@ -package org.navalplanner.business.common.daos.impl; - -import java.io.Serializable; -import java.lang.reflect.Method; -import java.lang.reflect.ParameterizedType; -import java.util.List; - -import org.hibernate.LockMode; -import org.hibernate.Session; -import org.hibernate.SessionFactory; -import org.hibernate.StaleObjectStateException; -import org.hibernate.criterion.Projections; -import org.hibernate.criterion.Restrictions; -import org.navalplanner.business.common.daos.IGenericDao; -import org.navalplanner.business.common.exceptions.InstanceNotFoundException; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.orm.hibernate3.HibernateCallback; -import org.springframework.orm.hibernate3.HibernateTemplate; - -// FIXME: This class is not currently used. I prefer GenericDaoHibernate, since -// it represents a non-intrusive use of Spring. - -/** - * An implementation of IGenericDao based on Spring's - * HibernateTemplate. Concrete DAOs must extend directly from - * this class. This constraint is imposed by the constructor of this class that - * must infer the type of the entity from the concrete DAO declaration.

- * - * This class autowires a SessionFactory bean and allows to - * implement Spring's HibernateTemplate-based DAOs. Subclasses access - * HibernateTemplate by calling on - * getHibernateTemplate() method. - * - * @author Fernando Bellas Permuy - * - * @param Entity class - * @param Primary key class - */ -public class GenericDaoHibernateTemplate implements - IGenericDao { - - private Class entityClass; - - private HibernateTemplate hibernateTemplate; - - @SuppressWarnings("unchecked") - public GenericDaoHibernateTemplate() { - this.entityClass = (Class) ((ParameterizedType) getClass() - .getGenericSuperclass()).getActualTypeArguments()[0]; - } - - protected HibernateTemplate getHibernateTemplate() { - return hibernateTemplate; - } - - @Autowired - public void setSessionFactory(SessionFactory sessionFactory) { - hibernateTemplate = new HibernateTemplate(sessionFactory); - } - - public void save(E entity) { - hibernateTemplate.saveOrUpdate(entity); - } - - public E merge(E entity) { - return entityClass.cast(hibernateTemplate.merge(entity)); - } - - public void checkVersion(E entity) { - - /* Get id and version from entity. */ - Serializable entityId; - long entityVersion; - - try { - - Method getIdMethod = entityClass.getMethod("getId"); - entityId = (Serializable) getIdMethod.invoke(entity); - - if (entityId == null) { - return; - } - - Method getVersionMethod = entityClass.getMethod("getVersion"); - entityVersion = (Long) getVersionMethod.invoke(entity); - - } catch (Exception e) { - throw new RuntimeException(e); - } - - /* Check version. */ - final Serializable id = entityId; - final long versionValueInMemory = entityVersion; - - hibernateTemplate.execute(new HibernateCallback() { - - public Object doInHibernate(Session session) { - - Long versionValueInDB = (Long) - session.createCriteria(entityClass). - add(Restrictions.idEq(id)). - setProjection(Projections.property("version")). - uniqueResult(); - - if (versionValueInDB == null) { - return null; - } - - if (versionValueInMemory != versionValueInDB) { - throw new StaleObjectStateException(entityClass.getName(), - id); - } - - return null; - - } - }); - - } - - public void lock(E entity) { - hibernateTemplate.lock(entity, LockMode.UPGRADE); - } - - @SuppressWarnings("unchecked") - public E find(PK id) throws InstanceNotFoundException { - - E entity = (E) hibernateTemplate.get(entityClass, id); - - if (entity == null) { - throw new InstanceNotFoundException(id, entityClass.getName()); - } - - return entity; - - } - - public boolean exists(final PK id) { - - return (Boolean) hibernateTemplate.execute(new HibernateCallback() { - public Object doInHibernate(Session session) { - return session.createCriteria(entityClass). - add(Restrictions.idEq(id)). - setProjection(Projections.id()). - uniqueResult() != null; - } - }); - - } - - public void remove(PK id) throws InstanceNotFoundException { - hibernateTemplate.delete(find(id)); - } - - @SuppressWarnings("unchecked") - @Override - public List list(Class klass) { - return hibernateTemplate.loadAll(klass); - } - -}