Example usage for org.hibernate SQLQuery setParameter

List of usage examples for org.hibernate SQLQuery setParameter

Introduction

In this page you can find the example usage for org.hibernate SQLQuery setParameter.

Prototype

@Override
    NativeQuery<T> setParameter(int position, Object val);

Source Link

Usage

From source file:cc.cnfc.core.orm.hibernate.SimpleHibernateDao.java

License:Open Source License

public SQLQuery createSQLQuery(final String queryString, final Object... values) {
    Assert.hasText(queryString, "queryString?");
    SQLQuery query = getSession().createSQLQuery(queryString);
    if (values != null) {
        for (int i = 0; i < values.length; i++) {
            query.setParameter(i, values[i]);
        }//from  w w w  .  j  av a  2 s .com
    }
    return query;
}

From source file:ch.algotrader.service.mysql.MySqlTransactionPersistenceServiceImpl.java

License:Open Source License

/**
 * {@inheritDoc}/* ww w.  j av a2 s . c o m*/
 */
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void ensurePositionAndCashBalance(final Transaction transaction) {

    Validate.notNull(transaction, "Transaction is null");

    transaction.initializeSecurity(HibernateInitializer.INSTANCE);

    Strategy strategy = transaction.getStrategy();
    Security security = transaction.getSecurity();
    Set<Currency> currencySet = new HashSet<>();
    Collection<CurrencyAmountVO> attributions = transaction.getAttributions();
    for (CurrencyAmountVO attribution : attributions) {
        currencySet.add(attribution.getCurrency());
    }

    Session currentSession = this.sessionFactory.getCurrentSession();

    if (security != null) {

        Serializable id = HibernateUtil.getNextId(this.sessionFactory, PositionImpl.class);

        SQLQuery sqlQuery = currentSession.createSQLQuery("INSERT IGNORE INTO position "
                + "  (id, quantity, cost, realized_p_l, persistent, security_fk, strategy_fk, version) "
                + "  VALUES (:position_id, 0, 0, 0, 0, :security_id, :strategy_id, 1)");

        sqlQuery.setParameter("position_id", id);
        sqlQuery.setParameter("security_id", security.getId());
        sqlQuery.setParameter("strategy_id", strategy.getId());
        sqlQuery.executeUpdate();
    }

    if (!currencySet.isEmpty()) {

        SQLQuery sqlQuery = currentSession.createSQLQuery("INSERT IGNORE INTO cash_balance "
                + "(currency, amount, strategy_fk, version) VALUES (:currency, 0, :strategy_id, 1)");
        for (Currency currency : currencySet) {

            sqlQuery.setParameter("currency", currency.name());
            sqlQuery.setParameter("strategy_id", strategy.getId());
            sqlQuery.executeUpdate();
        }
    }
}

From source file:cognition.pipeline.data.DNCWorkUnitDao.java

License:Apache License

/**
 * This method saves the conversion result in the specified saveTextToCoordinate
 * named query.//from w w  w .j ava  2s. c o  m
 * @param coordinate The original coordinate of the text
 */
public void saveConvertedText(DNCWorkCoordinate coordinate) {
    SessionWrapper sessionWrapper = createTargetSession();
    try {
        Query query = sessionWrapper.getNamedQuery("saveTextToCoordinate");
        String queryString = query.getQueryString();
        SQLQuery sqlQuery = sessionWrapper.createSQLQuery(queryString);
        sqlQuery.setParameter(0, coordinate.getPatientId());
        sqlQuery.setParameter(1, coordinate.getSourceTable());
        sqlQuery.setParameter(2, coordinate.getSourceColumn());
        sqlQuery.setParameter(3, coordinate.getIdInSourceTable());
        sqlQuery.setParameter(4, coordinate.getConversionResult());
        sqlQuery.executeUpdate();
    } finally {
        sessionWrapper.closeSession();
    }
}

From source file:com.abssh.util.GenericDao.java

License:Apache License

/**
 * SQL (=?)/*  ww  w  . j  av a 2 s. c om*/
 * 
 * @param sql
 *            SQL
 * @param param
 *            ?
 * @return ??object
 */
@SuppressWarnings("unchecked")
public List findList(String sql, Object... param) {
    SQLQuery query = getSession().createSQLQuery(sql);
    if (param != null && param.length > 0) {
        for (int i = 0; i < param.length; i++) {
            query.setParameter(i, param[i]);
        }
    }
    return query.list();
}

From source file:com.abssh.util.GenericDao.java

License:Apache License

/**
 * ?SQL// w  w  w  .  j a v  a  2s  .c  om
 * 
 * @param pageSize
 *            ??,0?
 * @param pageNo
 *            ??1?
 * @param sql
 *            SQL
 * @param param
 *            ?
 * @return ??object
 */
@SuppressWarnings("unchecked")
public List findList(int pageSize, int pageNo, String sql, Object... param) {
    if (pageSize <= 0 || pageNo <= 0) {
        // ?
        return findList(sql, param);
    }
    SQLQuery query = getSession().createSQLQuery(sql);
    query.setFirstResult(((pageNo - 1) * pageSize));
    query.setMaxResults(pageSize);
    if (param != null && param.length > 0) {
        for (int i = 0; i < param.length; i++) {
            query.setParameter(i, param[i]);
        }
    }
    return query.list();
}

From source file:com.abssh.util.GenericDao.java

License:Apache License

/**
 * SQL//from  ww  w.j ava 2s  .c  o  m
 * 
 * @param pageSize
 *            ??,0?
 * @param pageNo
 *            ??1?
 * @param sql
 *            SQL
 * @param clazz
 *            ?list
 * @param param
 *            ?
 * @return 
 */
@SuppressWarnings("unchecked")
public List findList(int pageSize, int pageNo, String sql, Class clazz, Object... param) {
    if (pageSize <= 0 || pageNo <= 0) {
        // ?
        return findList(sql, clazz, param);
    }
    SQLQuery query = getSession().createSQLQuery(sql).addEntity(clazz);
    query.setFirstResult(((pageNo - 1) * pageSize));
    query.setMaxResults(pageSize);
    if (param != null && param.length > 0) {
        for (int i = 0; i < param.length; i++) {
            query.setParameter(i, param[i]);
        }
    }
    return query.list();
}

From source file:com.abssh.util.GenericDao.java

License:Apache License

/**
 * SQL//from w  ww  .  j a v a 2s  .  c om
 * 
 * @param sql
 *            : SQL
 * @param param
 *             ?
 * @return ??
 */
public int executeSQL(String sql, Object... param) {
    SQLQuery query = getSession().createSQLQuery(sql);
    if (param != null && param.length > 0) {
        for (int i = 0; i < param.length; i++) {
            query.setParameter(i, param[i]);
        }
    }
    return query.executeUpdate();
}

From source file:com.algoTrader.entity.marketData.TickDaoBase.java

/**
 * {@inheritDoc}//w ww. j a  v  a 2  s .co m
 */
@Override
@SuppressWarnings("unchecked")
public Object findLastTickForSecurityAndMaxDate(final int transform, final String queryString,
        final int securityId, final Date maxDate) {
    try {
        SQLQuery queryObject = super.getSession(false).createSQLQuery(queryString);
        queryObject.addEntity(TickImpl.class);
        queryObject.setParameter("securityId", new Integer(securityId));
        queryObject.setParameter("maxDate", maxDate);
        Set results = new LinkedHashSet(queryObject.list());
        Object result = null;
        if (results.size() > 1) {
            throw new InvalidDataAccessResourceUsageException(
                    "More than one instance of 'com.algoTrader.entity.marketData.Tick"
                            + "' was found when executing query --> '" + queryString + "'");
        } else if (results.size() == 1) {
            result = results.iterator().next();
        }
        if (transform != TRANSFORM_NONE) {
            result = transformEntity(transform, (Tick) result);
        }
        return result;
    } catch (HibernateException ex) {
        throw super.convertHibernateAccessException(ex);
    }
}

From source file:com.autentia.wuija.persistence.impl.hibernate.HibernateDao.java

License:Open Source License

@Override
public void updateByNativeSQL(final String queryString, final Object... values) {
    getHibernateTemplate().execute(new HibernateCallback() {

        @Override//from  ww w .j av a 2s  . c  o  m
        public Object doInHibernate(Session session) throws HibernateException {
            final SQLQuery query = session.createSQLQuery(queryString);
            for (int i = 0; i < values.length; i++) {
                query.setParameter(i, values[i]);
            }
            return query.executeUpdate();
        }
    });
}

From source file:com.aw.core.db.support.WhereBuilder2.java

License:Open Source License

public void setParams(SQLQuery sqlQuery) {
    for (int i = 0; i < params.size(); i++) {
        Object param = params.get(i);
        if (param instanceof Long)
            sqlQuery.setLong(i, (Long) param);
        else if (param instanceof Integer)
            sqlQuery.setInteger(i, (Integer) param);
        else if (param instanceof Date)
            sqlQuery.setDate(i, (Date) param);
        else if (param instanceof String)
            sqlQuery.setString(i, (String) param);
        else if (param instanceof BigDecimal)
            sqlQuery.setBigDecimal(i, (BigDecimal) param);
        else if (param == null)
            sqlQuery.setParameter(i, null);
        else/*from ww w. ja  v  a2 s. co  m*/
            throw new IllegalArgumentException("Implementar codigo param:" + param.getClass());
    }
    //To change body of created methods use File | Settings | File Templates.
}