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.xpn.xwiki.plugin.query.HibernateQuery.java

License:Open Source License

protected List hqlexec(String hql, Map params, int fs, int fr) throws XWikiException {
    boolean bTransaction = true;
    final MonitorPlugin monitor = Util.getMonitorPlugin(getContext());
    List r = null;//from  w ww. j  a  v  a2 s. co  m
    try {
        // Start monitoring timer
        if (monitor != null)
            monitor.startTimer("hibernate");

        getHibernateStore().checkHibernate(getContext());
        bTransaction = getHibernateStore().beginTransaction(getContext());

        final Session ses = getHibernateStore().getSession(getContext());
        final Query q = ses.createQuery(hql);
        if (params != null && !params.isEmpty()) {
            for (Iterator iter = params.keySet().iterator(); iter.hasNext();) {
                final String element = (String) iter.next();
                final Object val = params.get(element);
                if (val instanceof Collection)
                    q.setParameterList(element, (Collection) val);
                else
                    q.setParameter(element, val);
            }
        }
        if (fs > 0)
            q.setMaxResults(fs);
        if (fr > 0)
            q.setFirstResult(fr);

        r = q.list();

        if (bTransaction)
            getHibernateStore().endTransaction(getContext(), false);
        return r;
    } catch (Throwable e) {
        Object[] args = { hql };
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE,
                XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SEARCH,
                "Exception while searching documents with sql {0}", e, args);
    } finally {
        try {
            if (bTransaction)
                getHibernateStore().endTransaction(getContext(), false);
        } catch (Exception e) {
        }
        // End monitoring timer
        if (monitor != null)
            monitor.endTimer("hibernate");
    }
}

From source file:com.yahoo.elide.datastores.hibernate3.HibernateTransaction.java

License:Apache License

/**
 * Builds a Hibernate query from a HQL fragment (containing SELECT & FROM) and a filter expression.
 * @param hqlQuery The HQL fragment/*from  www  .  jav  a  2  s  .c o m*/
 * @param expression the filter expression to expand into a WHERE clause.
 * @return an executable query.
 */
private Query populateWhereClause(String hqlQuery, FilterExpression expression) {
    String completeQuery = hqlQuery + " " + new HQLFilterOperation().apply(expression, true);

    Query query = session.createQuery(completeQuery);

    /* Extract the predicates from the expression */
    PredicateExtractionVisitor visitor = new PredicateExtractionVisitor();
    Set<FilterPredicate> predicates = expression.accept(visitor);

    /* Populate query parameters from each predicate*/
    for (FilterPredicate filterPredicate : predicates) {
        if (filterPredicate.getOperator().isParameterized()) {
            String name = filterPredicate.getParameterName();
            if (filterPredicate.isMatchingOperator()) {
                query = query.setParameter(name, filterPredicate.getStringValueEscaped("%", "\\"));
            } else {
                query = query.setParameterList(name, filterPredicate.getValues());
            }
        }
    }

    return query;
}

From source file:com.zhenhappy.ems.dao.imp.TVisaDaoImp.java

@Override
public List<TVisa> loadTVisasByVids(Integer[] vids) {
    Query q = this.getSession().createQuery(
            "select new TVisa(v.id, v.eid, v.passportName, v.gender, v.nationality, v.jobTitle, v.companyName, v.boothNo, v.detailedAddress, v.tel, v.fax, v.email, v.companyWebsite, v.passportNo, v.expDate, v.birth, v.applyFor, v.from, v.to, v.passportPage, v.businessLicense, v.joinerId, v.status, v.createTime, v.updateTime) from TVisa v where v.id in (:vids)");
    q.setParameterList("vids", vids);
    return q.list();
}

From source file:com.zhenhappy.ems.dao.imp.WVisaDaoImp.java

@Override
public List<WVisa> loadWVisasByVids(Integer[] vids) {
    Query q = this.getSession().createQuery(
            "select new WVisa(v.id,v.WCustomerInfo,v.fullPassportName,v.gender,v.nationality,v.passportNo,v.expDate,v.dateOfBirth,v.chineseEmbassy,v.consulateOfCity,v.durationBeginTime,v.durationEndTime,v.passportPage,v.businessLicense,v.wthInfoId,v.createTime,v.updateTime,v.needPost,v.expressTp,v.expressNo,v.isDisabled) from WVisa v where v.id in (:vids)");
    q.setParameterList("vids", vids);
    return q.list();
}

From source file:com.zutubi.pulse.master.model.persistence.hibernate.HibernateAgentDailyStatisticsDao.java

License:Apache License

public int deleteByAgentNotIn(final Set<Long> agentIds) {
    return getHibernateTemplate().execute(new HibernateCallback<Integer>() {
        public Integer doInHibernate(Session session) throws HibernateException {
            Query queryObject = session
                    .createQuery("delete from AgentDailyStatistics where agentId not in (:agentIds)");
            queryObject.setParameterList("agentIds", agentIds);
            return queryObject.executeUpdate();
        }/*from   www . j  av  a2s .  c  o m*/
    });
}

From source file:com.zutubi.pulse.master.model.persistence.hibernate.HibernateBuildResultDao.java

License:Apache License

public int getCompletedResultCount(final User user) {
    return toInt(getHibernateTemplate().execute(new HibernateCallback<Long>() {
        public Long doInHibernate(Session session) throws HibernateException {
            Query queryObject = session.createQuery(
                    "select count (*) from BuildResult model where model.user = :user and model.stateName in (:stateNames)");
            queryObject.setEntity("user", user);
            queryObject.setParameterList("stateNames", getStateNames(ResultState.getCompletedStates()));
            return (Long) queryObject.uniqueResult();
        }// w  ww.  jav a2  s.co m
    }));
}

From source file:com.zutubi.pulse.master.model.persistence.hibernate.HibernateBuildResultDao.java

License:Apache License

public List<BuildResult> getOldestCompletedBuilds(final User user, final int offset) {
    int count = getCompletedResultCount(user);
    if (count > offset) {
        final int max = count - offset;

        return getHibernateTemplate().execute(new HibernateCallback<List<BuildResult>>() {
            public List<BuildResult> doInHibernate(Session session) throws HibernateException {
                Query queryObject = session.createQuery(
                        "from BuildResult model where model.user = :user and model.stateName in (:stateNames) order by model.number asc");
                queryObject.setEntity("user", user);
                queryObject.setParameterList("stateNames", getStateNames(ResultState.getCompletedStates()));
                if (max > 0) {
                    queryObject.setMaxResults(max);
                }/*w  ww  .  j  a  va  2 s. com*/
                return queryObject.list();
            }
        });
    }

    return Collections.emptyList();
}

From source file:com.zutubi.pulse.master.model.persistence.hibernate.HibernateBuildResultDao.java

License:Apache License

public int getBuildCountByAgentName(final String agent, final Project[] projects, final ResultState[] states) {
    return getHibernateTemplate().execute(new HibernateCallback<Integer>() {
        public Integer doInHibernate(Session session) throws HibernateException {
            Long count = getHibernateTemplate().execute(new HibernateCallback<Long>() {
                public Long doInHibernate(Session session) throws HibernateException {
                    Query queryObject = session
                            .createQuery("select count(distinct result) from BuildResult result "
                                    + "  join result.stages stage " + "where stage.agentName = :agent"
                                    + (projects == null ? "" : " and result.project in (:projects)")
                                    + (states == null ? "" : " and result.stateName in (:states)"));

                    queryObject.setString("agent", agent);
                    if (projects != null) {
                        queryObject.setParameterList("projects", projects);
                    }//from  w  w w .j a v a2  s.  c om

                    if (states != null) {
                        queryObject.setParameterList("states", getStateNames(states));
                    }

                    return (Long) queryObject.uniqueResult();
                }
            });

            return count.intValue();
        }
    });
}

From source file:com.zutubi.pulse.master.model.persistence.hibernate.HibernateBuildResultDao.java

License:Apache License

public List<BuildResult> findLatestByAgentName(final String agent, final Project[] projects,
        final ResultState[] states, final int first, final int max) {
    return getHibernateTemplate().execute(new HibernateCallback<List<BuildResult>>() {
        public List<BuildResult> doInHibernate(Session session) throws HibernateException {
            Query queryObject = session.createQuery("select distinct result from BuildResult result "
                    + "  join result.stages stage " + "where stage.agentName = :agent "
                    + (projects == null ? "" : " and result.project in (:projects)")
                    + (states == null ? "" : "and result.stateName in (:states) ") + "order by result.id desc");

            queryObject.setString("agent", agent);
            if (projects != null) {
                queryObject.setParameterList("projects", projects);
            }/*from   w w  w .  j  a va2s . c o m*/

            if (states != null) {
                queryObject.setParameterList("states", getStateNames(states));
            }

            if (first >= 0) {
                queryObject.setFirstResult(first);
            }

            if (max >= 0) {
                queryObject.setMaxResults(max);
            }

            return queryObject.list();
        }
    });
}

From source file:com.zutubi.pulse.master.model.persistence.hibernate.HibernateChangelistDao.java

License:Apache License

public List<PersistentChangelist> findLatestByUser(final User user, final int max) {
    final Set<String> allLogins = new HashSet<String>();
    allLogins.add(user.getLogin());/*  w  w  w.ja  va2 s .  c o  m*/
    allLogins.add(user.getName());
    allLogins.addAll(user.getPreferences().getAliases());

    return findUnique(new ChangelistQuery() {
        public Query createQuery(Session session) {
            Query queryObject = session.createQuery(
                    "from PersistentChangelist model where model.author in (:logins) order by model.time desc, model.id desc");
            queryObject.setParameterList("logins", allLogins);
            return queryObject;
        }
    }, max);
}