Example usage for org.hibernate Query setParameterList

List of usage examples for org.hibernate Query setParameterList

Introduction

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

Prototype

Query<R> setParameterList(int position, Object[] values);

Source Link

Usage

From source file:com.allinfinance.commquery.dao.CommQueryDAO.java

License:Open Source License

public List findBySQLQuery(final String sql, final Map map) {
    List data = getHibernateTemplate().executeFind(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createSQLQuery(sql);
            Iterator iter = map.keySet().iterator();
            while (iter.hasNext()) {
                String key = iter.next().toString();
                Object obj = map.get(key);
                String[] keys = query.getNamedParameters();
                for (int i = 0; i < keys.length; i++) {
                    if (key != null && key.equals(keys[i])) {
                        if (obj instanceof String) {
                            query.setString(key, obj.toString());
                        }/*www.j  a v a2s.  c  o  m*/
                        if (obj instanceof Number) {
                            query.setInteger(key, Integer.parseInt(obj.toString()));
                        }
                        if (obj instanceof BigDecimal) {
                            query.setBigDecimal(key, (BigDecimal) obj);
                        }
                        if (obj instanceof List) {
                            query.setParameterList(key, (List) obj);
                        }
                    }
                }
            }
            return query.list();
        }
    });
    return data;
}

From source file:com.allinfinance.commquery.dao.CommQueryDAO.java

License:Open Source License

public List findBySQLQuery(final String sql, final int begin, final int count, final Map map) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createSQLQuery(sql);
            Iterator iter = map.keySet().iterator();
            while (iter.hasNext()) {
                String key = iter.next().toString();
                Object obj = map.get(key);
                String[] keys = query.getNamedParameters();
                for (int i = 0; i < keys.length; i++) {
                    if (key != null && key.equals(keys[i])) {
                        if (obj instanceof String) {
                            query.setString(key, obj.toString());
                        }/*w w  w  .  j  a va2s .c  o m*/
                        if (obj instanceof Number) {
                            query.setInteger(key, Integer.parseInt(obj.toString()));
                        }
                        if (obj instanceof BigDecimal) {
                            query.setBigDecimal(key, (BigDecimal) obj);
                        }
                        if (obj instanceof List) {
                            query.setParameterList(key, (List) obj);
                        }
                    }
                }
            }
            if (begin >= 0) {
                query.setFirstResult(begin);
                query.setMaxResults(count);
            }
            return query.list();
        }
    });
}

From source file:com.allinfinance.commquery.dao.CommQueryDAO.java

License:Open Source License

public String findCountBySQLQuery(final String countSql, final Map map) {
    List data = getHibernateTemplate().executeFind(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createSQLQuery(countSql);
            Iterator iter = map.keySet().iterator();
            while (iter.hasNext()) {
                String key = iter.next().toString();
                Object obj = map.get(key);
                String[] keys = query.getNamedParameters();
                for (int i = 0; i < keys.length; i++) {
                    if (key != null && key.equals(keys[i])) {
                        if (obj instanceof String) {
                            query.setString(key, obj.toString());
                        }/*from  w w  w .ja v  a 2 s  .  co  m*/
                        if (obj instanceof Number) {
                            query.setInteger(key, Integer.parseInt(obj.toString()));
                        }
                        if (obj instanceof BigDecimal) {
                            query.setBigDecimal(key, (BigDecimal) obj);
                        }
                        if (obj instanceof List) {
                            query.setParameterList(key, (List) obj);
                        }
                    }
                }
            }
            return query.list();
        }
    });
    return data.get(0).toString();
}

From source file:com.amalto.core.storage.hibernate.HibernateStorage.java

License:Open Source License

@SuppressWarnings("rawtypes")
private void deleteData(ComplexTypeMetadata typeToDelete, Map<String, List> condition, TypeMapping mapping) {
    Session session = this.getCurrentSession();
    for (FieldMetadata field : typeToDelete.getFields()) {
        if (field.isMany()) {
            String formattedTableName = tableResolver.getCollectionTable(field);
            String deleteFormattedTableSQL = "delete from " + formattedTableName; //$NON-NLS-1$
            if (!condition.isEmpty()) {
                deleteFormattedTableSQL = deleteFormattedTableSQL + " where " + conditionMapToString(condition); //$NON-NLS-1$                    
            }//from  ww w.j  a  v a2s .co m
            session.createSQLQuery(deleteFormattedTableSQL).executeUpdate();
        }
    }
    // Delete the type instances
    String className = storageClassLoader.getClassFromType(typeToDelete).getName();
    String hql = "delete from " + className; //$NON-NLS-1$
    org.hibernate.Query query = session.createQuery(hql);
    if (!condition.isEmpty()) {
        hql = hql + " where "; //$NON-NLS-1$
        for (Entry<String, List> fieldEntry : condition.entrySet()) {
            if (!hql.endsWith("where ")) { //$NON-NLS-1$
                hql = hql + " and "; //$NON-NLS-1$
            }
            hql = hql + fieldEntry.getKey() + " in (:" + fieldEntry.getKey() + ")"; //$NON-NLS-1$//$NON-NLS-2$
        }
        query = session.createQuery(hql);
        for (Entry<String, List> fieldEntry : condition.entrySet()) {
            query.setParameterList(fieldEntry.getKey(), fieldEntry.getValue());
        }
    }
    query.executeUpdate();
    // Clean up full text indexes
    if (dataSource.supportFullText()) {
        FullTextSession fullTextSession = Search.getFullTextSession(session);
        Set<Class<?>> indexedTypes = fullTextSession.getSearchFactory().getIndexedTypes();
        Class<? extends Wrapper> entityType = storageClassLoader.getClassFromType(mapping.getDatabase());
        if (indexedTypes.contains(entityType)) {
            fullTextSession.purgeAll(entityType);
        } else {
            LOGGER.warn("Unable to delete full text indexes for '" + entityType + "' (not indexed)."); //$NON-NLS-1$ //$NON-NLS-2$
        }
    }
}

From source file:com.autentia.intra.dao.hibernate.HibernateManagerBase.java

License:Open Source License

/**
 * Perform a custom Hibernate HQL search.
 *
 * @param hql the hql query/* w  w w  . j av a  2 s .  c o  m*/
 * @return a List of Objects with the result
 * @throws DataAccException
 * @oaram args query parameters
 */
@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.currentSession();
        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();
        session.getTransaction().commit();

        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;
}

From source file:com.autentia.tnt.dao.hibernate.HibernateManagerBase.java

License:Open Source License

/**
 * Perform a custom Hibernate HQL search.
 * @param hql the hql query/*from  ww w  . j av a 2  s .c  o  m*/
 * @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;
}

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

License:Open Source License

private <T> List<T> prepareFindByQueryWithListParameters(Query query, int firstResult, int maxResults,
        Object[] values) {/*from  w w w. ja va 2 s  .c om*/
    setPagination(query, firstResult, maxResults);
    for (int i = 0; i < values.length; i++) {
        if (values[i] instanceof List) {
            query.setParameterList("list" + i, (Collection) values[i]);
        } else {
            query.setParameter("list" + i, values[i]);
        }

    }
    return query.list();
}

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

License:Open Source License

@Override
public Integer bulkUpdateWithInStatementSupport(final String hqlQuery, final Object... values) {
    return (Integer) getHibernateTemplate().execute(new HibernateCallback() {

        @Override/*from  w ww .  j  a v a2 s . c o  m*/
        public Object doInHibernate(Session session) throws HibernateException {
            final Query query = session.createQuery(hqlQuery);
            for (int i = 0; i < values.length; i++) {
                if (values[i] instanceof List<?>) {
                    query.setParameterList("param" + i, (List<?>) values[i]);
                } else {
                    query.setParameter("param" + i, values[i]);
                }
            }

            return Integer.valueOf(query.executeUpdate());
        }
    });
}

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

License:Open Source License

private void addParamsToQueryCheckingIfIsListType(final Query query, Object[] params) {
    for (int i = 0; i < params.length; i++) {
        if (params[i] instanceof Collection) {
            query.setParameterList("param" + i, (Collection) params[i]);
        } else {/*from  w w w.ja  v a2s  .c  om*/
            query.setParameter("param" + i, params[i]);
        }
    }
}

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

License:Open Source License

@SuppressWarnings("unchecked")
private void setNamedParametersWithListSupport(final Map values, final Query query) {
    final Iterator<String> it = values.keySet().iterator();
    while (it.hasNext()) {
        final String key = it.next();
        final Object value = values.get(key);
        if (value instanceof List) {
            query.setParameterList(key, (Collection) value);
        } else {/* w w w .  ja v  a2s  . c  o m*/
            query.setParameter(key, value);
        }
    }
}