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, Type type);

Source Link

Usage

From source file:ch.algotrader.dao.AbstractDaoTest.java

License:Open Source License

@Test
public void testFindByQueryCollectionParameter() throws Exception {

    GenericItem stuff1 = new GenericItem("this");
    stuff1.setActive(true);//from   w w w  . j  a  va 2  s.  c om
    stuff1.setBroker(Broker.DC.name());

    this.dao.save(stuff1);

    long id1 = stuff1.getId();

    Assert.assertNotEquals(0, id1);

    GenericItem stuff2 = new GenericItem("that");
    stuff2.setActive(true);
    stuff2.setBroker(Broker.IB.name());

    this.dao.save(stuff2);

    long id2 = stuff2.getId();

    Assert.assertNotEquals(0, id2);

    GenericItem stuff3 = new GenericItem("this and that");
    stuff3.setActive(true);
    stuff3.setBroker(Broker.RT.name());

    this.dao.save(stuff3);

    long id3 = stuff3.getId();

    Assert.assertNotEquals(0, id3);

    this.dao.flush();

    Query query = this.dao.prepareQuery(null, "select s from GenericItem s where s.id in (:ids)",
            QueryType.HQL);
    query.setParameterList("ids", Arrays.asList(stuff1.getId(), stuff2.getId(), stuff3.getId()),
            LongType.INSTANCE);

    List<?> list1 = query.list();

    Assert.assertNotNull(list1);

    Assert.assertEquals(3, list1.size());
}

From source file:com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery.java

License:Apache License

public Query getAsHqlQuery(Session session) {
    String text = getAsHqlText(0);
    LOGGER.trace("HQL text generated:\n{}", text);
    Query query = session.createQuery(text);
    for (Map.Entry<String, QueryParameterValue> parameter : parameters.entrySet()) {
        String name = parameter.getKey();
        QueryParameterValue parameterValue = parameter.getValue();
        LOGGER.trace("Parameter {} = {}", name, parameterValue.debugDump());

        if (parameterValue.getValue() instanceof Collection) {
            if (parameterValue.getType() != null) {
                query.setParameterList(name, (Collection) parameterValue.getValue(), parameterValue.getType());
            } else {
                query.setParameterList(name, (Collection) parameterValue.getValue());
            }/* www  .j  a v  a 2 s. c o  m*/
        } else {
            if (parameterValue.getType() != null) {
                query.setParameter(name, parameterValue.getValue(), parameterValue.getType());
            } else {
                query.setParameter(name, parameterValue.getValue());
            }
        }
    }
    if (maxResults != null) {
        query.setMaxResults(maxResults);
    }
    if (firstResult != null) {
        query.setFirstResult(firstResult);
    }
    if (resultTransformer != null) {
        query.setResultTransformer(resultTransformer);
    }
    return query;
}

From source file:com.knowbout.epg.entities.Schedule.java

License:Apache License

@SuppressWarnings("unchecked")
public static List<NetworkSchedule> selectNextPrograms(List<String> programId, Date startDate) {
    Session session = HibernateUtil.currentSession();
    Query query = session.getNamedQuery("Schedule.getAllNextProgramSchedulesForList");
    query.setTimestamp("startTime", startDate);
    query.setParameterList("programIds", programId, new StringType());
    return query.list();
}

From source file:com.knowbout.epg.entities.Schedule.java

License:Apache License

@SuppressWarnings("unchecked")
public static List<NetworkSchedule> selectNextPrograms(String lineup, List<String> programId, Date startDate) {
    Session session = HibernateUtil.currentSession();
    Query query = session.getNamedQuery("Schedule.getNextProgramSchedulesForList");
    query.setTimestamp("startTime", startDate);
    query.setParameterList("programIds", programId, new StringType());
    query.setString("lineup", lineup);
    return query.list();
}

From source file:com.knowbout.epg.entities.Schedule.java

License:Apache License

@SuppressWarnings("unchecked")
public static List<NetworkSchedule> selectLastPrograms(String lineup, List<String> programId, Date startDate) {
    Session session = HibernateUtil.currentSession();
    Query query = session.getNamedQuery("Schedule.getLastProgramSchedulesForList");
    query.setTimestamp("startTime", startDate);
    query.setParameterList("programIds", programId, new StringType());
    query.setString("lineup", lineup);
    return query.list();
}

From source file:lucee.runtime.orm.hibernate.HibernateORMSession.java

License:Open Source License

private Object __executeQuery(PageContext pc, Session session, Key dsn, String hql, Object params,
        boolean unique, Struct options) throws PageException {
    //Session session = getSession(pc,null);
    hql = hql.trim();/*from   w  ww  .j  av  a2 s.  c o m*/
    org.hibernate.Query query = session.createQuery(hql);
    // options
    if (options != null) {
        // maxresults
        Object obj = options.get("maxresults", null);
        if (obj != null) {
            int max = CommonUtil.toIntValue(obj, -1);
            if (max < 0)
                throw ExceptionUtil.createException(this, null, "option [maxresults] has an invalid value ["
                        + obj + "], value should be a number bigger or equal to 0", null);
            query.setMaxResults(max);
        }
        // offset
        obj = options.get("offset", null);
        if (obj != null) {
            int off = CommonUtil.toIntValue(obj, -1);
            if (off < 0)
                throw ExceptionUtil.createException(this, null, "option [offset] has an invalid value [" + obj
                        + "], value should be a number bigger or equal to 0", null);
            query.setFirstResult(off);
        }
        // readonly
        obj = options.get("readonly", null);
        if (obj != null) {
            Boolean ro = CommonUtil.toBoolean(obj, null);
            if (ro == null)
                throw ExceptionUtil.createException(this, null,
                        "option [readonly] has an invalid value [" + obj + "], value should be a boolean value",
                        null);
            query.setReadOnly(ro.booleanValue());
        }
        // timeout
        obj = options.get("timeout", null);
        if (obj != null) {
            int to = CommonUtil.toIntValue(obj, -1);
            if (to < 0)
                throw ExceptionUtil.createException(this, null, "option [timeout] has an invalid value [" + obj
                        + "], value should be a number bigger or equal to 0", null);
            query.setTimeout(to);
        }
    }

    // params
    if (params != null) {
        QueryPlanCache cache = data.getQueryPlanCache(dsn);
        HQLQueryPlan plan = cache.getHQLQueryPlan(hql, false, java.util.Collections.EMPTY_MAP);
        ParameterMetadata meta = plan.getParameterMetadata();
        Type type;
        Object obj;

        // struct
        if (CommonUtil.isStruct(params)) {
            Struct sct = CommonUtil.toStruct(params);
            Key[] keys = CommonUtil.keys(sct);
            String name;
            // fix case-senstive
            Struct names = CommonUtil.createStruct();
            if (meta != null) {
                Iterator<String> it = meta.getNamedParameterNames().iterator();
                while (it.hasNext()) {
                    name = it.next();
                    names.setEL(name, name);
                }
            }

            RefBoolean isArray = CommonUtil.createRefBoolean();
            for (int i = 0; i < keys.length; i++) {
                obj = sct.get(keys[i], null);
                if (meta != null) {
                    name = (String) names.get(keys[i], null);
                    if (name == null)
                        continue; // param not needed will be ignored
                    type = meta.getNamedParameterExpectedType(name);
                    obj = HibernateCaster.toSQL(type, obj, isArray);
                    if (isArray.toBooleanValue()) {
                        if (obj instanceof Object[])
                            query.setParameterList(name, (Object[]) obj, type);
                        else if (obj instanceof List)
                            query.setParameterList(name, (List) obj, type);
                        else
                            query.setParameterList(name, Caster.toList(obj), type);
                    } else
                        query.setParameter(name, obj, type);

                } else
                    query.setParameter(keys[i].getString(), obj);
            }
        }

        // array
        else if (CommonUtil.isArray(params)) {
            Array arr = CommonUtil.toArray(params);
            Iterator it = arr.valueIterator();
            int index = 0;
            SQLItem item;
            RefBoolean isArray = null;
            while (it.hasNext()) {
                obj = it.next();
                if (obj instanceof SQLItem) {
                    item = (SQLItem) obj;
                    obj = item.getValue();
                    //HibernateCaster.toHibernateType(item.getType(), null); MUST
                    //query.setParameter(index, item.getValue(),type);
                }
                if (meta != null) {
                    type = meta.getOrdinalParameterExpectedType(index + 1);
                    obj = HibernateCaster.toSQL(type, obj, isArray);
                    // TOOD can the following be done somehow
                    //if(isArray.toBooleanValue())
                    //   query.setParameterList(index, (Object[])obj,type);
                    //else
                    query.setParameter(index, obj, type);
                } else
                    query.setParameter(index, obj);
                index++;
            }
            if (meta.getOrdinalParameterCount() > index)
                throw ExceptionUtil.createException(this, null, "parameter array is to small [" + arr.size()
                        + "], need [" + meta.getOrdinalParameterCount() + "] elements", null);
        }
    }

    // select
    String lcHQL = hql.toLowerCase();
    if (lcHQL.startsWith("select") || lcHQL.startsWith("from")) {
        if (unique) {
            return uniqueResult(query);
        }

        return query.list();
    }
    // update
    return new Double(query.executeUpdate());
}