Example usage for org.hibernate Query setProperties

List of usage examples for org.hibernate Query setProperties

Introduction

In this page you can find the example usage for org.hibernate Query setProperties.

Prototype

Query<R> setProperties(Map bean);

Source Link

Document

Bind the values of the given Map for each named parameters of the query, matching key names with parameter names and mapping value types to Hibernate types using heuristics.

Usage

From source file:sncode.java.hibernate.dao.GenericDao.java

License:MIT License

/**
 * Run a Native SQL Query supporting NamedParameters
 * @param sql_      String with Native SQL Query
 * @param params_   Map with Named Parameters for SQL Query
 * @param session_  Hibernate Session Object
 * @return List of Objects with query result
 * @throws DaoException /*from  www . j a v a2  s .c  o  m*/
 * 
 */
public List sqlQuery(String sql_, Map params_, Session session_) throws DaoException {
    Transaction tx = null;
    boolean globaltx = true;
    List result = null;
    try {
        if (session_ == null) {
            session_ = this.startSession();
        }
        if (session_.getTransaction() == null || !session_.getTransaction().isActive()) {
            tx = session_.beginTransaction();
            globaltx = false;
        }

        Query query = session_.createSQLQuery(sql_);

        if (params_ != null)
            query.setProperties(params_);

        result = query.list();

        if (!globaltx) {
            tx.commit();
        }

    } catch (Exception e) {
        logger.error("Error running sqlquery: " + stacktraceError(e));
        throw new DaoException("Error running sqlquery: " + stacktraceError(e));
    } finally {
        if (!globaltx) {
            if (session_ != null && session_.isOpen()) {
                if (tx != null && tx.isActive()) {
                    session_.flush();
                }
                session_.close();
                session_ = null;
            }
        }
    }
    return result;
}

From source file:sncode.java.hibernate.dao.GenericDao.java

License:MIT License

/**
 * Run HQL Queries//from w  w  w . j  a  v a  2s.c  o  m
 * @param hql_      HQL string
 * @param params_   Map with named parameters
 * @param session_  Hibernate session
 * @return
 * @throws DaoException 
 */
public List hqlQuery(String hql_, Map params_, Session session_) throws DaoException {
    Transaction tx = null;
    boolean globaltx = true;
    List result = null;
    try {
        if (session_ == null) {
            session_ = this.startSession();
        }
        if (session_.getTransaction() == null || !session_.getTransaction().isActive()) {
            tx = session_.beginTransaction();
            globaltx = false;
        }

        Query query = session_.createQuery(hql_);

        if (params_ != null)
            query.setProperties(params_);

        result = query.list();

        if (!globaltx) {
            tx.commit();
        }

    } catch (Exception e) {
        logger.error("Error running hqlquery: " + stacktraceError(e));
        throw new DaoException("Error running hqlquery: " + stacktraceError(e));
    } finally {
        if (!globaltx) {
            if (session_ != null && session_.isOpen()) {
                if (tx != null && tx.isActive()) {
                    session_.flush();
                }
                session_.close();
                session_ = null;
            }
        }
    }
    return result;
}