1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
public abstract class AbstractDaoImpl extends HibernateDaoSupport implements IAbstractDao {
public abstract String getDomainObjectName();
public abstract String getCamelCaseDomainObjectName();
public abstract Class getDomainObjectClass();
public AbstractDomainObject findById(Long id) throws DaoException, ObjectNotFoundException, CheckException {
return findById(id, true);
}
public AbstractDomainObject findById(Long id, boolean checkSoftDelete) throws DaoException, ObjectNotFoundException, CheckException {
if (id == null) {
throw new CheckException("l'id est obligatoire");
}
try {
AbstractDomainObject object = (AbstractDomainObject) getHibernateTemplate().load(getDomainObjectClass(), id);
if (object == null) {
throw new ObjectNotFoundException();
}
return object;
} catch (Exception e) {
throw new DaoException("ne peut pas recuperer l'objet. ", e);
}
}
/**
* generic count method used to retrieve the total object entities count
*
* @return int count
*/
public int count() throws DaoException, CheckException {
LOG.debug("entering "+getDomainObjectName()+"DAO.count()");
Criteria criteria = getSession().createCriteria(getDomainObjectClass());
criteria.add(Restrictions.gt("id", 0L));
criteria.setProjection(Projections.rowCount());
List result = criteria.list();
return ((Integer) result.get(0)).intValue();
}
/**
* generic count method used to retrieve the total object entities count with search criterias
*
* @param String[] criterias
* @param String[] values
* @return int count
*/
public int count(String[] criterias, String[] values) throws DaoException, CheckException {
LOG.debug("entering "+getDomainObjectName()+"DAO.count()");
Criteria criteria = getSession().createCriteria(getDomainObjectClass());
criteria.add(Restrictions.gt("id", 0L));
criteria.setProjection(Projections.rowCount());
if(criterias!=null){
for(int i=0;i<criterias.length;i++)
{
criteria.add(Restrictions.eq(criterias[i],values[i]));
}
}
List result = criteria.list();
return ((Integer) result.get(0)).intValue();
}
/**
* generic insert method used to insert a single object entity
*
* @param AbstractDomainObject object
* @return AbstractDomainObject
*/
public AbstractDomainObject insert(AbstractDomainObject object) throws DaoException, DuplicateKeyException, CheckException {
LOG.debug("entering "+getDomainObjectName()+"DAO.insert");
try {
// set creation values
object.setId((Long)getHibernateTemplate().save(object));
getHibernateTemplate().flush();
return object;
}
catch (GenericJDBCException ge) {
if ((ge.getCause() != null)
&& (ge.getCause() instanceof SQLException)) {
SQLException sqlException = (SQLException) ge.getCause();
if (sqlException.getErrorCode() == TpmConstantes.DATA_ALREADY_EXIST) {
throw new DuplicateKeyException();
} else {
throw new DaoException(ge);
}
} else {
throw new DaoException(ge);
}
} catch (DataAccessException e) {
throw new DaoException(e);
}catch (ConstraintViolationException cve) {
throw new DaoException(cve);
}
}
public AbstractDomainObject insertAndEvict(AbstractDomainObject value) throws Exception
{
AbstractDomainObject object = insert(value);
getHibernateTemplate().evict(object);
return object;
}
/**
* generic insert method used to delete a single object entity
*
* @param AbstractDomainObject object
* @return AbstractDomainObject
*/
public long delete(long id) throws DaoException,ObjectNotFoundException,CheckException {
//LOG.debug("entering "+getDomainObjectName()+"DAO.delete("+id)+")");
try {
AbstractDomainObject entity = (AbstractDomainObject)getSession().load(getDomainObjectClass(), id);
getSession().delete(entity);
//getHibernateTemplate().delete(object);
return entity.getId();
}
catch (DataAccessException e)
{
throw new DaoException(e);
}
}
/**
* generic update method used to delete a single object entity
*
* @param AbstractDomainObject object
* @return AbstractDomainObject
*/
public AbstractDomainObject update(AbstractDomainObject object) throws CheckException, DaoException, ObjectNotFoundException {
LOG.debug("entering "+getDomainObjectName()+"DAO.update("+object.getId()+")");
try {
getHibernateTemplate().saveOrUpdate(object);
getHibernateTemplate().flush();
return object;
}
catch (DataAccessException e)
{
throw new DaoException(e);
}
} |
Partager