Java Code Examples for org.hibernate.SQLQuery#setParameter()

The following examples show how to use org.hibernate.SQLQuery#setParameter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EmpDaoImpl.java    From ignite-book-code-samples with GNU General Public License v3.0 7 votes vote down vote up
@Override
@CacheEvict(value = "exchangeRate", key = "#e.region")
public void updateExchange(ExchangeRate e) {
    Session session = sessionFactory.openSession();
    session.getTransaction().begin();
    SQLQuery query =  session.createSQLQuery("update exchangerate \n" +
            " set usdollar = :usdollar" +
            " where region = :region and ratedate = TO_DATE('2015-05-02','YYYY-MM-DD')") ;

    query.setParameter("region", e.getRegion());
    query.setParameter("usdollar", e.getUsdollar());
    query.addEntity(ExchangeRate.class);
    query.executeUpdate();
    session.getTransaction().commit();
    session.close();
}
 
Example 2
Source File: UnlimitedMessageColumnsMigration.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isMigrationNeeded(Session session, String dbName) {
    SQLQuery conditionalSqlQuery = session.createSQLQuery(MYSQL_CHECK_MIGRATION_QUERY);
    conditionalSqlQuery.setParameter(0, dbName);
    List<?> conditionalSqlQueryResult = conditionalSqlQuery.list();
    if (CollectionUtils.isEmpty(conditionalSqlQueryResult)) {
        return false;
    }
    return !StringUtils.equalsIgnoreCase("longblob", (String)conditionalSqlQueryResult.get(0));
}
 
Example 3
Source File: EmpDaoImpl.java    From ignite-book-code-samples with GNU General Public License v3.0 5 votes vote down vote up
@Override
@Cacheable(value = "exchangeRate")
public String getExchangeRateByRegion(String region) {
    Session session = sessionFactory.openSession();
    // in real life, it should be current date time
    SQLQuery query = session.createSQLQuery("select * from exchangerate e where e.ratedate = TO_DATE('2015-05-02','YYYY-MM-DD') and e.region=:region");
    query.setParameter("region", region);
    query.addEntity(ExchangeRate.class);
    ExchangeRate res =  (ExchangeRate)query.uniqueResult();
    session.close();
    return String.valueOf(res.getUsdollar());
}
 
Example 4
Source File: BaseHibernateDao.java    From framework with Apache License 2.0 5 votes vote down vote up
/**
 * Description: <br>
 * 
 * @author 王伟<br>
 * @taskId <br>
 * @param procedureSql
 * @param params
 * @return
 * @throws DaoException <br>
 */

@Override
public <T> List<T> executeProcedure(final String procedureSql, final Object... params) throws DaoException {
    Session session = getSession();
    session.flush();
    SQLQuery sqlQuery = session.createSQLQuery(procedureSql);

    for (int i = 0; i < params.length; i++) {
        sqlQuery.setParameter(i, params[i]);
    }
    return sqlQuery.list();
}
 
Example 5
Source File: HibernateAccess.java    From snakerflow with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> List<T> queryList(Class<T> clazz, String sql, Object... args) {
	SQLQuery query = getSession().createSQLQuery(sql);
	query.addEntity(clazz);
	for(int i = 0; i < args.length; i++) {
		query.setParameter(i, args[i]);
	}
	return (List<T>)query.list();
}
 
Example 6
Source File: HibernateAccess.java    From snakerflow with Apache License 2.0 5 votes vote down vote up
public Object queryCount(String sql, Object... args) {
    SQLQuery countQuery = getSession().createSQLQuery(sql);
    if(args.length > 0) {
        for (int i = 0; i < args.length; i++) {
            countQuery.setParameter(i, args[i]);
        }
    }
    return countQuery.uniqueResult();
}
 
Example 7
Source File: BaseDao.java    From JgFramework with Apache License 2.0 5 votes vote down vote up
protected SQLQuery getQuery(String queryString, Map<String, Object> where, int start, int limit, boolean returnMap) {
    Session session = this.getSession();
    SQLQuery query = null;
    if (start > -1 && limit > 0) {
        query = session.createSQLQuery(queryString);
        query.setFirstResult(start);
        query.setMaxResults(limit);
    } else {
        query = session.createSQLQuery(queryString);
    }
    if (where != null) {
        for (Entry<String, Object> entry : where.entrySet()) {
            Object value = entry.getValue();
            String key = entry.getKey();
            if (value instanceof Collection) {
                query.setParameterList(key, (Collection) value);
            } else if (value instanceof Object[]) {
                query.setParameterList(key, (Object[]) value);
            } else {
                query.setParameter(key, value);
            }
        }
    }
    if (returnMap) {
        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
    }
    return query;
}
 
Example 8
Source File: GenericBaseCommonDao.java    From jeecg with Apache License 2.0 5 votes vote down vote up
/**
 * 调用存储过程
 */
@SuppressWarnings({ "unchecked",})
public <T> List<T> executeProcedure(String executeSql,Object... params) {
	SQLQuery sqlQuery = getSession().createSQLQuery(executeSql);
	
	for(int i=0;i<params.length;i++){
		sqlQuery.setParameter(i, params[i]);
	}
	
	return sqlQuery.list();
}
 
Example 9
Source File: HibernateAccess.java    From snakerflow with Apache License 2.0 4 votes vote down vote up
public Integer getLatestProcessVersion(String name) {
	SQLQuery query = getSession().createSQLQuery(QUERY_VERSION + " where name = ?");
	query.setParameter(0, name);
	Object result = query.uniqueResult();
	return new Long(ClassHelper.castLong(result)).intValue();
}