ItEr20S04ArquitecturaServidorItEr19S04: Adding automatical exception translation, so calling convertHibernateAccessException(e) is no longer necessary.
This commit is contained in:
parent
3e8d8e98d0
commit
79b798519f
5 changed files with 31 additions and 89 deletions
|
|
@ -1,6 +1,5 @@
|
|||
package org.navalplanner.business.advance.daos;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.navalplanner.business.advance.entities.AdvanceType;
|
||||
import org.navalplanner.business.common.daos.GenericDAOHibernate;
|
||||
|
|
@ -17,14 +16,8 @@ import org.springframework.stereotype.Repository;
|
|||
public class AdvanceTypeDAO extends GenericDAOHibernate<AdvanceType, Long>
|
||||
implements IAdvanceTypeDAO {
|
||||
public boolean existsNameAdvanceType(String unitName) {
|
||||
try {
|
||||
|
||||
return getSession().createCriteria(AdvanceType.class).add(
|
||||
Restrictions.eq("unitName", unitName)).uniqueResult() != null;
|
||||
|
||||
} catch (HibernateException e) {
|
||||
throw convertHibernateAccessException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -12,4 +12,5 @@ public interface IAdvanceTypeDAO extends IGenericDAO<AdvanceType, Long>{
|
|||
public boolean existsNameAdvanceType(String unitName);
|
||||
|
||||
public AdvanceType findByName(String name);
|
||||
|
||||
}
|
||||
|
|
@ -6,7 +6,6 @@ import java.lang.reflect.ParameterizedType;
|
|||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.LockMode;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
|
|
@ -15,8 +14,6 @@ import org.hibernate.criterion.Projections;
|
|||
import org.hibernate.criterion.Restrictions;
|
||||
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.orm.hibernate3.SessionFactoryUtils;
|
||||
|
||||
/**
|
||||
* An implementation of <code>IGenericDao</code> based on Hibernate's native
|
||||
|
|
@ -59,40 +56,19 @@ public class GenericDAOHibernate<E, PK extends Serializable> implements
|
|||
return sessionFactory.getCurrentSession();
|
||||
}
|
||||
|
||||
protected DataAccessException convertHibernateAccessException(
|
||||
HibernateException e) {
|
||||
|
||||
return SessionFactoryUtils.convertHibernateAccessException(e);
|
||||
|
||||
}
|
||||
|
||||
public void save(E entity) {
|
||||
|
||||
try {
|
||||
getSession().saveOrUpdate(entity);
|
||||
} catch (HibernateException e) {
|
||||
throw convertHibernateAccessException(e);
|
||||
}
|
||||
|
||||
getSession().saveOrUpdate(entity);
|
||||
}
|
||||
|
||||
public void reattachUnmodifiedEntity(E entity) {
|
||||
|
||||
try {
|
||||
getSession().lock(entity, LockMode.NONE);
|
||||
} catch (HibernateException e) {
|
||||
throw convertHibernateAccessException(e);
|
||||
}
|
||||
getSession().lock(entity, LockMode.NONE);
|
||||
|
||||
}
|
||||
|
||||
public E merge(E entity) {
|
||||
|
||||
try {
|
||||
return entityClass.cast(getSession().merge(entity));
|
||||
} catch (HibernateException e) {
|
||||
throw convertHibernateAccessException(e);
|
||||
}
|
||||
return entityClass.cast(getSession().merge(entity));
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -123,55 +99,36 @@ public class GenericDAOHibernate<E, PK extends Serializable> implements
|
|||
}
|
||||
|
||||
/* Check version. */
|
||||
try {
|
||||
Long versionValueInDB = (Long) getSession().createCriteria(entityClass)
|
||||
.add(Restrictions.idEq(id)).setProjection(
|
||||
Projections.property("version")).uniqueResult();
|
||||
|
||||
Long versionValueInDB = (Long)
|
||||
getSession().createCriteria(entityClass).
|
||||
add(Restrictions.idEq(id)).
|
||||
setProjection(Projections.property("version")).
|
||||
uniqueResult();
|
||||
if (versionValueInDB == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (versionValueInDB == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!versionValueInMemory.equals(versionValueInDB)) {
|
||||
throw new StaleObjectStateException(entityClass.getName(), id);
|
||||
}
|
||||
|
||||
} catch (HibernateException e) {
|
||||
throw convertHibernateAccessException(e);
|
||||
if (!versionValueInMemory.equals(versionValueInDB)) {
|
||||
throw new StaleObjectStateException(entityClass.getName(), id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void lock(E entity) {
|
||||
|
||||
try {
|
||||
getSession().lock(entity, LockMode.UPGRADE);
|
||||
} catch (HibernateException e) {
|
||||
throw convertHibernateAccessException(e);
|
||||
}
|
||||
getSession().lock(entity, LockMode.UPGRADE);
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public E find(PK id) throws InstanceNotFoundException {
|
||||
|
||||
try {
|
||||
E entity = (E) getSession().get(entityClass, id);
|
||||
|
||||
E entity = (E) getSession().get(entityClass, id);
|
||||
|
||||
if (entity == null) {
|
||||
throw new InstanceNotFoundException(id, entityClass.getName());
|
||||
}
|
||||
|
||||
return entity;
|
||||
|
||||
} catch (HibernateException e) {
|
||||
throw convertHibernateAccessException(e);
|
||||
if (entity == null) {
|
||||
throw new InstanceNotFoundException(id, entityClass.getName());
|
||||
}
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
public E findExistingEntity(PK id) {
|
||||
|
|
@ -186,39 +143,25 @@ public class GenericDAOHibernate<E, PK extends Serializable> implements
|
|||
|
||||
public boolean exists(final PK id) {
|
||||
|
||||
try {
|
||||
|
||||
return getSession().createCriteria(entityClass).
|
||||
add(Restrictions.idEq(id)).
|
||||
setProjection(Projections.id()).
|
||||
uniqueResult() != null;
|
||||
|
||||
} catch (HibernateException e) {
|
||||
throw convertHibernateAccessException(e);
|
||||
}
|
||||
return getSession().createCriteria(entityClass).add(
|
||||
Restrictions.idEq(id)).setProjection(Projections.id())
|
||||
.uniqueResult() != null;
|
||||
|
||||
}
|
||||
|
||||
public void remove(PK id) throws InstanceNotFoundException {
|
||||
|
||||
try {
|
||||
getSession().delete(find(id));
|
||||
} catch (HibernateException e) {
|
||||
throw convertHibernateAccessException(e);
|
||||
}
|
||||
|
||||
getSession().delete(find(id));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T extends E> List<T> list(Class<T> klass) {
|
||||
return getSession().createCriteria(klass).list();
|
||||
}
|
||||
|
||||
try {
|
||||
return getSession().createCriteria(klass).list();
|
||||
} catch (HibernateException e) {
|
||||
throw convertHibernateAccessException(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
getSession().flush();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -108,4 +108,6 @@ public interface IGenericDAO <E, PK extends Serializable>{
|
|||
|
||||
public <T extends E> List<T> list(Class<T> klass);
|
||||
|
||||
public void flush();
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,9 @@
|
|||
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"
|
||||
p:jndiName="${dataSource.jndiName}" p:resourceRef="true" />
|
||||
|
||||
<!-- Letting Spring do automatically exception translation -->
|
||||
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
|
||||
|
||||
<!-- Hibernate Session Factory. -->
|
||||
<bean id="sessionFactory"
|
||||
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue