Search in sources :

Example 1 with DataAccException

use of com.autentia.tnt.dao.DataAccException in project TNTConcept by autentia.

the class HibernateManagerBase method insert.

/**
 * Inserta un objeto de negocio en base de datos
 *
 * @param obj a insertar
 */
public void insert(T obj) throws DataAccException {
    log.debug("insert");
    Session session = null;
    try {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        Date d = new Date();
        obj.setInsertDate(d);
        obj.setUpdateDate(d);
        session.save(obj);
        log.debug("objeto correctamente insertado");
    } catch (Exception ex) {
        log.error("insert - exception", ex);
        StringBuffer msg = new StringBuffer("Error insertando el objeto: ");
        msg.append(obj);
        throw new DataAccException(msg.toString(), ex);
    }
}
Also used : DataAccException(com.autentia.tnt.dao.DataAccException) Date(java.util.Date) DataAccException(com.autentia.tnt.dao.DataAccException) Session(org.hibernate.Session)

Example 2 with DataAccException

use of com.autentia.tnt.dao.DataAccException in project TNTConcept by autentia.

the class HibernateManagerBase method search.

/**
 * Perform a custom Hibernate HQL search.
 * @param hql the hql query
 * @oaram args query parameters
 * @return a List of Objects with the result
 * @throws DataAccException
 */
@SuppressWarnings("unchecked")
protected List search(String hql, Object... args) throws DataAccException {
    log.debug("search - hql='" + hql + "'");
    Session session = null;
    List<Object> ret = null;
    try {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        if (!session.getTransaction().isActive()) {
            session.beginTransaction();
        }
        Query q = session.createQuery(hql);
        for (int i = 0; i < args.length; i++) {
            if (args[i] instanceof Collection) {
                q.setParameterList("arg" + i, (Collection) args[i]);
            } else {
                q.setParameter("arg" + i, args[i]);
            }
        }
        ret = q.list();
        log.debug("search - found " + ret.size() + " objects");
    } catch (Exception ex) {
        log.error("search - error searching", ex);
        throw new DataAccException("Error searching '" + hql + "'", ex);
    }
    return ret;
}
Also used : DataAccException(com.autentia.tnt.dao.DataAccException) Query(org.hibernate.Query) Collection(java.util.Collection) IDataAccessObject(com.autentia.tnt.dao.IDataAccessObject) ITransferObject(com.autentia.tnt.dao.ITransferObject) DataAccException(com.autentia.tnt.dao.DataAccException) Session(org.hibernate.Session)

Example 3 with DataAccException

use of com.autentia.tnt.dao.DataAccException in project TNTConcept by autentia.

the class HibernateManagerBase method update.

/**
 * Actualiza una objeto de negocio en base de datos
 *
 * @param obj el objeto de negocio a actualizar
 */
public void update(ITransferObject obj, Serializable pk) throws DataAccException {
    log.debug("update");
    Session session = null;
    try {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        obj.setUpdateDate(new Date());
        session.merge(obj);
        log.debug("objeto con clave '" + pk + "' correctamente actualizado");
        if (this.cacheable)
            HibernateManagerBase.removeCacheFor(obj.getClass().getName());
    } catch (Exception ex) {
        String msg = "Error actualizando el objeto '" + obj + "' con clave primaria '" + pk + "'";
        log.debug("error actualizando el objeto '" + pk + "': " + msg);
        throw new DataAccException(msg, ex);
    }
}
Also used : DataAccException(com.autentia.tnt.dao.DataAccException) Date(java.util.Date) DataAccException(com.autentia.tnt.dao.DataAccException) Session(org.hibernate.Session)

Example 4 with DataAccException

use of com.autentia.tnt.dao.DataAccException in project TNTConcept by autentia.

the class HibernateManagerBase method search.

/**
 * Get DAOs matching searching criteria
 * @param <T> type of entity the manager works with
 * @param clazz entity class
 * @param search search criteria
 * @param sort sort criteria (can be null)
 * @return the list of objects matching the criteria
 * @throws DataAccException
 */
protected List<T> search(Class<T> clazz, SearchCriteria search, SortCriteria sort) throws DataAccException {
    log.debug("search - clazz='" + clazz + "' search='" + search + "' sort='" + sort + "'");
    List<T> ret = null;
    try {
        // Append FROM condition
        StringBuilder query = new StringBuilder("FROM ");
        query.append(clazz.getName());
        query.append(" p");
        // Append WHERE condition
        if (search != null) {
            query.append(" ");
            query.append(search.getHQL());
        }
        // Append ORDER BY condition
        if (sort != null) {
            query.append(" ");
            query.append(sort.getHQL());
        }
        // Make query
        ret = (List<T>) search(query.toString(), (search == null) ? new Object[0] : search.getArguments());
        log.debug("search - found " + ret.size() + " objects of type " + clazz.getName());
    } catch (Exception ex) {
        log.error("search - error searching", ex);
        throw new DataAccException("Error searching " + clazz.getName() + " objects", ex);
    }
    return ret;
}
Also used : DataAccException(com.autentia.tnt.dao.DataAccException) IDataAccessObject(com.autentia.tnt.dao.IDataAccessObject) ITransferObject(com.autentia.tnt.dao.ITransferObject) DataAccException(com.autentia.tnt.dao.DataAccException)

Example 5 with DataAccException

use of com.autentia.tnt.dao.DataAccException in project TNTConcept by autentia.

the class HibernateManagerBase method delete.

/**
 * Elimina un objeto de negocio de base de datos
 * @deprecated use this method only from derived classes, and use delete() methods in derived classes from other places
 * @param obj el objeto de negocio a eliminar
 * @param pk clave primaria del objeto de negocio a eliminar
 */
public void delete(Object obj, Serializable pk) throws DataAccException {
    log.debug("delete");
    Class clazz = obj.getClass();
    Session session = null;
    try {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        // Cargamos el objeto antiguo
        Object objOld = session.load(clazz, pk);
        log.debug("objeto con clave '" + pk + "' recuperado");
        // Borramos
        session.delete(objOld);
        log.debug("objeto con clave '" + pk + "' correctamente eliminado");
        if (this.cacheable)
            HibernateManagerBase.removeCacheFor(clazz.getName());
        doAfterDelete(pk);
    } catch (Exception ex) {
        StringBuffer msg = new StringBuffer("Error borrando el objeto '");
        msg.append(pk);
        log.debug("error eliminando el objeto '" + pk + "': " + msg.toString());
        throw new DataAccException(msg.toString(), ex);
    }
}
Also used : DataAccException(com.autentia.tnt.dao.DataAccException) IDataAccessObject(com.autentia.tnt.dao.IDataAccessObject) ITransferObject(com.autentia.tnt.dao.ITransferObject) DataAccException(com.autentia.tnt.dao.DataAccException) Session(org.hibernate.Session)

Aggregations

DataAccException (com.autentia.tnt.dao.DataAccException)12 Session (org.hibernate.Session)8 ITransferObject (com.autentia.tnt.dao.ITransferObject)4 Date (java.util.Date)4 IDataAccessObject (com.autentia.tnt.dao.IDataAccessObject)3 DataIntegrityException (com.autentia.tnt.dao.DataIntegrityException)2 DataNotFoundException (com.autentia.tnt.dao.DataNotFoundException)2 SQLException (java.sql.SQLException)2 Setting (com.autentia.tnt.businessobject.Setting)1 User (com.autentia.tnt.businessobject.User)1 SettingSearch (com.autentia.tnt.dao.search.SettingSearch)1 SecException (com.autentia.tnt.manager.security.exception.SecException)1 Version (com.autentia.tnt.version.Version)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Collection (java.util.Collection)1 List (java.util.List)1 Locale (java.util.Locale)1 GrantedAuthority (org.acegisecurity.GrantedAuthority)1