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.qcadoo.model.internal.search.SearchQueryImpl.java

License:Open Source License

@Override
public void addParameters(final Query query) {
    for (Map.Entry<String, String> parameter : strings.entrySet()) {
        query.setString(parameter.getKey(), parameter.getValue());
    }//from   w  ww . j  a v  a2 s.  c o m
    for (Map.Entry<String, Boolean> parameter : booleans.entrySet()) {
        query.setBoolean(parameter.getKey(), parameter.getValue());
    }
    for (Map.Entry<String, Byte> parameter : bytes.entrySet()) {
        query.setByte(parameter.getKey(), parameter.getValue());
    }
    for (Map.Entry<String, Short> parameter : shorts.entrySet()) {
        query.setShort(parameter.getKey(), parameter.getValue());
    }
    for (Map.Entry<String, Integer> parameter : integers.entrySet()) {
        query.setInteger(parameter.getKey(), parameter.getValue());
    }
    for (Map.Entry<String, Long> parameter : longs.entrySet()) {
        query.setLong(parameter.getKey(), parameter.getValue());
    }
    for (Map.Entry<String, Float> parameter : floats.entrySet()) {
        query.setFloat(parameter.getKey(), parameter.getValue());
    }
    for (Map.Entry<String, Double> parameter : doubles.entrySet()) {
        query.setDouble(parameter.getKey(), parameter.getValue());
    }
    for (Map.Entry<String, BigDecimal> parameter : bigDecimals.entrySet()) {
        query.setBigDecimal(parameter.getKey(), parameter.getValue());
    }
    for (Map.Entry<String, Date> parameter : dates.entrySet()) {
        query.setDate(parameter.getKey(), parameter.getValue());
    }
    for (Map.Entry<String, Date> parameter : times.entrySet()) {
        query.setTime(parameter.getKey(), parameter.getValue());
    }
    for (Map.Entry<String, Date> parameter : timestamps.entrySet()) {
        query.setTimestamp(parameter.getKey(), parameter.getValue());
    }
    for (Map.Entry<String, Object> parameter : parameters.entrySet()) {
        query.setParameter(parameter.getKey(), parameter.getValue());
    }
    for (Map.Entry<String, Collection<? extends Object>> parametersList : parameterLists.entrySet()) {
        query.setParameterList(parametersList.getKey(), parametersList.getValue());
    }
    for (Map.Entry<String, Object> parameter : entities.entrySet()) {
        query.setEntity(parameter.getKey(), parameter.getValue());
    }
}

From source file:com.querydsl.jpa.hibernate.HibernateUtil.java

License:Apache License

private static void setValue(Query query, String key, Object val) {
    if (val instanceof Collection<?>) {
        query.setParameterList(key, (Collection<?>) val);
    } else if (val instanceof Object[] && !BUILT_IN.contains(val.getClass())) {
        query.setParameterList(key, (Object[]) val);
    } else if (NUMERIC_TYPES.containsKey(val.getClass())) {
        query.setParameter(key, val, NUMERIC_TYPES.get(val.getClass()));
    } else {//from   w  w w .jav a  2  s .  c om
        query.setParameter(key, val);
    }
}

From source file:com.redhat.rhn.common.hibernate.HibernateFactory.java

License:Open Source License

/**
 * Binds the values of the map to a named query parameter, whose value
 * matches the key in the given Map, guessing the Hibernate type from the
 * class of the given object./*from   w  ww . j a  v  a 2  s  .  c  o  m*/
 * @param query Query to be modified.
 * @param parameters named query parameters to be bound.
 * @return Modified Query.
 * @throws HibernateException if there is a problem with updating the Query.
 * @throws ClassCastException if the key in the given Map is NOT a String.
 */
private Query bindParameters(Query query, Map parameters) throws HibernateException {
    if (parameters == null) {
        return query;
    }

    Set entrySet = parameters.entrySet();
    for (Iterator itr = entrySet.iterator(); itr.hasNext();) {
        Map.Entry entry = (Map.Entry) itr.next();
        if (entry.getValue() instanceof Collection) {
            Collection c = (Collection) entry.getValue();
            if (c.size() > 1000) {
                LOG.error("Query executed with Collection larger than 1000");
            }
            query.setParameterList((String) entry.getKey(), c);
        } else {
            query.setParameter((String) entry.getKey(), entry.getValue());
        }
    }

    return query;
}

From source file:com.redhat.rhn.domain.action.ActionFactory.java

License:Open Source License

/**
 * Recursively query the hierarchy of actions dependent on a given
 * parent. While recursive, only one query is executed per level in
 * the hierarchy, and action hierarchies tend to not be more than
 * two levels deep.//from w w  w  .  j  a va 2s.co m
 *
 * @param parentAction Parent action.
 * @return Set of actions dependent on the given parent.
 */
public static Set lookupDependentActions(Action parentAction) {
    Session session = HibernateFactory.getSession();

    Set returnSet = new HashSet();
    List actionsAtHierarchyLevel = new LinkedList();
    actionsAtHierarchyLevel.add(parentAction.getId());
    do {
        Query findDependentActions = session.getNamedQuery("Action.findDependentActions");
        findDependentActions.setParameterList("action_ids", actionsAtHierarchyLevel);
        List results = findDependentActions.list();
        returnSet.addAll(results);
        // Reset list of actions for the next hierarchy level:
        actionsAtHierarchyLevel = new LinkedList();
        for (Iterator i = results.iterator(); i.hasNext();) {
            actionsAtHierarchyLevel.add(((Action) i.next()).getId());
        }
    } while (actionsAtHierarchyLevel.size() > 0);

    return returnSet;
}

From source file:com.redhat.rhn.domain.action.ActionFactory.java

License:Open Source License

/**
 * Delete the server actions associated with the given set of parent actions.
 * @param parentActions Set of parent actions.
 *///from   w  w  w .j  a  v  a 2s .c  o  m
public static void deleteServerActionsByParent(Set parentActions) {
    Session session = HibernateFactory.getSession();

    Query serverActionsToDelete = session.getNamedQuery("ServerAction.deleteByParentActions");
    serverActionsToDelete.setParameterList("actions", parentActions);
    serverActionsToDelete.executeUpdate();
}

From source file:com.redhat.rhn.domain.kickstart.KickstartFactory.java

License:Open Source License

/**
 * Fail the kickstart sessions associated with the given actions and servers.
 *
 * @param actionsToDelete Actions associated with the kickstart sessions to fail.
 * @param servers Servers assocaited with the kickstart sessions to fail.
 *//*from w w w  .  ja  v a2  s  .  c  o  m*/
public static void failKickstartSessions(Set actionsToDelete, Set servers) {
    Session session = HibernateFactory.getSession();
    Iterator iter;
    KickstartSessionState failed = KickstartFactory.SESSION_STATE_FAILED;
    Query kickstartSessionQuery = session.getNamedQuery("KickstartSession.findPendingForActions");
    kickstartSessionQuery.setParameterList("servers", servers);
    kickstartSessionQuery.setParameterList("actions_to_delete", actionsToDelete);

    List ksSessions = kickstartSessionQuery.list();
    iter = ksSessions.iterator();
    while (iter.hasNext()) {
        KickstartSession ks = (KickstartSession) iter.next();
        log.debug("Failing kickstart associated with action: " + ks.getId());
        ks.setState(failed);
        ks.setAction(null);

        setKickstartSessionHistoryMessage(ks, failed, KICKSTART_CANCELLED_MESSAGE);
    }
}

From source file:com.redhat.rhn.domain.server.ServerFactory.java

License:Open Source License

/**
 * Looks up server objects from the given list of server IDs.
 *
 * If more than 1000 servers are present in the list we'll split it into
 * chunks as this can cause problems on Oracle.
 *
 * @param serverIds List of server IDs.//from  w w w.j  a  v a 2 s. co  m
 * @param user who wants to lookup the Server
 * @return list of server objects
 */
public static List<Server> lookupByIdsAndUser(List<Long> serverIds, User user) {
    Session session = HibernateFactory.getSession();
    Query query = session.getNamedQuery("Server.findByIdsAndOrgId").setParameter("orgId",
            user.getOrg().getId());
    if (serverIds.size() < 1000) {
        query.setParameterList("serverIds", serverIds);
        return query.list();
    }

    List<Server> results = new LinkedList<Server>();
    List<Long> blockOfIds = new LinkedList<Long>();
    for (Long sid : serverIds) {
        blockOfIds.add(sid);
        if (blockOfIds.size() == 999) {
            query.setParameterList("serverIds", blockOfIds);
            results.addAll(query.list());
            blockOfIds = new LinkedList<Long>();
        }
    }
    // Deal with the remainder:
    if (blockOfIds.size() > 0) {
        query.setParameterList("serverIds", blockOfIds);
        results.addAll(query.list());
    }
    return results;
}

From source file:com.redhat.rhn.domain.server.ServerFactory.java

License:Open Source License

/**
 * Lookup Servers by their ids//from  w  ww  .  j  ava2  s .  c  om
 * @param ids the ids to search for
 * @return the Servers found
 */
public static List<Server> lookupByIds(List<Long> ids) {
    Session session = HibernateFactory.getSession();
    Query query = session.getNamedQuery("Server.findByIds");
    List<Server> results = new LinkedList<Server>();

    if (ids.size() == 0) {
        return results;
    }

    if (ids.size() < 1000) {
        query.setParameterList("serverIds", ids);
        return query.list();
    }

    List<Long> blockOfIds = new LinkedList<Long>();
    for (Long sid : ids) {
        blockOfIds.add(sid);
        if (blockOfIds.size() == 999) {
            query.setParameterList("serverIds", blockOfIds);
            results.addAll(query.list());
            blockOfIds = new LinkedList<Long>();
        }
    }
    // Deal with the remainder:
    if (blockOfIds.size() > 0) {
        query.setParameterList("serverIds", blockOfIds);
        results.addAll(query.list());
    }
    return results;
}

From source file:com.salesmanager.core.service.catalog.impl.db.dao.ProductDao.java

License:Open Source License

private SearchProductResponse findAvailableProductsByProductsIdAndLanguageId(SearchProductCriteria criteria,
        List<Long> productIds, int languageId) {

    try {/*from  w  w w .  j  a  v  a  2  s  .c  o m*/

        /*
         * List list =super.getSession().createQuery(
         * "select p from Product p left join fetch p.descriptions s left join fetch p.specials y left join fetch p.prices r left join fetch r.priceDescriptions rd left join fetch r.special x where p.productId in(:pId) and s.id.languageId=:lId and p.productDateAvailable is not null and p.productDateAvailable<=:dt order by p.productSortOrder"
         * ) .setParameterList("pId", productIds) .setInteger("lId",
         * languageId) .setDate("dt", new Date())
         * .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY) .list();
         * 
         * return list;
         */

        SearchProductResponse response = new SearchProductResponse();

        if (productIds == null || productIds.size() == 0) {
            response.setCount(0);
            response.setProducts(new ArrayList());
            return response;
        }

        /** count total values **/
        Query c = super.getSession().createQuery(
                "select count(p) from Product p left join p.descriptions s where p.productId in(:pId) and s.id.languageId=:lId and p.productDateAvailable is not null and p.productDateAvailable<=:dt order by p.productSortOrder");
        // Query c =
        // super.getSession().createQuery("select count(p) from Product p left join p.descriptions s where p.merchantId=:mId and p.masterCategoryId in(:cId) and s.id.languageId=:lId");
        c.setParameterList("pId", productIds).setInteger("lId", languageId).setDate("dt", new Date());

        int count = ((Number) c.uniqueResult()).intValue();
        response.setCount(count);

        Query q = super.getSession().createQuery(
                "select p from Product p left join fetch p.descriptions s left join fetch p.specials y left join fetch p.prices r left join fetch r.priceDescriptions rd left join fetch r.special x where p.productId in(:pId) and s.id.languageId=:lId and p.productDateAvailable is not null and p.productDateAvailable<=:dt order by p.productSortOrder");
        q.setParameterList("pId", productIds).setInteger("lId", languageId).setDate("dt", new Date())
                .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

        if (count > 0) {
            q.setFirstResult(criteria.getLowerLimit());
            q.setMaxResults(criteria.getUpperLimit(count));
        }

        List l = q.list();

        response.setProducts(l);

        return response;

    } catch (RuntimeException re) {
        log.error("get failed", re);
        throw re;
    }
}

From source file:com.sos.jobnet.db.EventsDBLayer.java

License:Apache License

private Query setQueryParams(EventsFilter filter, String hql) {
    Query query = null;
    try {/*from  w  w  w  .  j  a v a2  s . co m*/
        query = session.createQuery(hql);
        if (filter.hasEvents())
            query.setParameterList(EVENT_ID, filter.getEventList());
        if (filter.hasEventClass())
            query.setParameter(EVENT_CLASS, filter.getEventClass());
    } catch (HibernateException e) {
        throw new JobSchedulerException("Error creating Query", e);
    }
    return query;
}