Example usage for org.hibernate Query setParameter

List of usage examples for org.hibernate Query setParameter

Introduction

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

Prototype

@SuppressWarnings("unchecked")
Query<R> setParameter(int position, Object val);

Source Link

Document

Bind a positional query parameter using its inferred Type.

Usage

From source file:br.com.suricattus.surispring.hibernate.criterion.HQL.java

License:Open Source License

@SuppressWarnings("rawtypes")
private Query getQuery() {

    StringBuilder hql = new StringBuilder();

    if (!select.isEmpty())
        append(hql, "select", select, ",");
    append(hql, "from", from);
    if (!where.isEmpty())
        append(hql, "where", where, "and");
    if (!group.isEmpty())
        append(hql, "group by", group, ",");
    if (!order.isEmpty())
        append(hql, "order by", order, ",");

    Query query = session.createQuery(hql.toString());

    for (String paramName : params.keySet()) {
        Object param = params.get(paramName);
        if (param instanceof Collection) {
            query.setParameterList(paramName, (Collection) params.get(paramName));
        } else if (param instanceof Object[]) {
            query.setParameterList(paramName, (Object[]) params.get(paramName));
        } else {/*from   w  w w  . j  a  v  a  2  s.c  om*/
            query.setParameter(paramName, params.get(paramName));
        }
    }

    if (firstResult != null)
        query.setFirstResult(firstResult);
    if (maxResults != null)
        query.setMaxResults(maxResults);

    return query;
}

From source file:br.com.thiaguten.persistence.spi.provider.hibernate.HibernatePersistenceProvider.java

License:Apache License

/**
 * {@inheritDoc}/*w  w w. j a va2  s . c o m*/
 */
@Override
public <ID extends Serializable, T extends Persistable<ID>> List<T> findByNamedQuery(Class<T> entityClazz,
        String queryName, Object... params) {
    Query hibernateQuery = getSession().getNamedQuery(queryName);
    if (params != null) {
        for (int i = 0; i < params.length; i++) {
            hibernateQuery.setParameter(i + 1, params[i]);
        }
    }
    return hibernateQuery.list();
}

From source file:br.com.thiaguten.persistence.spi.provider.hibernate.HibernatePersistenceProvider.java

License:Apache License

/**
 * {@inheritDoc}/* w ww.  ja  v  a 2  s. co  m*/
 */
@Override
public <ID extends Serializable, T extends Persistable<ID>> List<T> findByNamedQueryAndNamedParams(
        Class<T> entityClazz, String queryName, Map<String, ?> params) {
    Query hibernateQuery = getSession().getNamedQuery(queryName);
    if (params != null && !params.isEmpty()) {
        for (final Map.Entry<String, ?> param : params.entrySet()) {
            hibernateQuery.setParameter(param.getKey(), param.getValue());
        }
    }
    return hibernateQuery.list();
}

From source file:br.com.thiaguten.persistence.spi.provider.hibernate.HibernatePersistenceProvider.java

License:Apache License

/**
 * {@inheritDoc}//w ww  .jav a2 s  .co  m
 */
@Override
public <ID extends Serializable, T extends Persistable<ID>> List<T> findByQueryAndNamedParams(
        Class<T> entityClazz, String query, Map<String, ?> params) {
    Query hibernateQuery = getSession().createQuery(query);
    if (params != null && !params.isEmpty()) {
        for (final Map.Entry<String, ?> param : params.entrySet()) {
            hibernateQuery.setParameter(param.getKey(), param.getValue());
        }
    }
    return hibernateQuery.list();
}

From source file:br.com.thiaguten.persistence.spi.provider.hibernate.HibernatePersistenceProvider.java

License:Apache License

/**
 * {@inheritDoc}//from   w w  w.  jav  a 2s .  c  o m
 */
@Override
public <T extends Number> T countByNamedQueryAndNamedParams(Class<T> resultClazz, String queryName,
        Map<String, ?> params) {
    Query hibernateQuery = getSession().getNamedQuery(queryName);
    if (params != null && !params.isEmpty()) {
        for (final Map.Entry<String, ?> param : params.entrySet()) {
            hibernateQuery.setParameter(param.getKey(), param.getValue());
        }
    }
    return (T) hibernateQuery.uniqueResult();
}

From source file:br.com.thiaguten.persistence.spi.provider.hibernate.HibernatePersistenceProvider.java

License:Apache License

/**
 * {@inheritDoc}//  www  .  j  ava  2 s . co m
 */
@Override
public <T extends Number> T countByQueryAndNamedParams(Class<T> resultClazz, String query,
        Map<String, ?> params) {
    Query hibernateQuery = getSession().createQuery(query);
    if (params != null && !params.isEmpty()) {
        for (final Map.Entry<String, ?> param : params.entrySet()) {
            hibernateQuery.setParameter(param.getKey(), param.getValue());
        }
    }
    return (T) hibernateQuery.uniqueResult();
}

From source file:br.gov.jfrj.siga.wf.dao.WfDao.java

License:Open Source License

public List<TaskInstance> consultarTarefasAtivasPorDocumento(String siglaDoc) {
    Query query = getSessao().getNamedQuery("consultarTarefasAtivasPorDocumento");
    query.setParameter("siglaDoc", siglaDoc + "%");
    return query.list();
}

From source file:br.gov.jfrj.siga.wf.dao.WfDao.java

License:Open Source License

public WfConhecimento consultarConhecimento(String procedimento, String tarefa) {
    Query query = getSessao().getNamedQuery("consultarConhecimento");
    query.setParameter("procedimento", procedimento);
    query.setParameter("tarefa", tarefa);
    List<WfConhecimento> l = query.list();
    if (l.size() == 0)
        return null;
    return l.get(0);

}

From source file:br.gov.jfrj.siga.wf.service.impl.WfServiceImpl.java

License:Open Source License

@Override
public Object variavelPorDocumento(String codigoDocumento, String nomeDaVariavel) throws Exception {
    try {/*from   w w  w . j  av  a2s.  c  o  m*/
        // TODO Auto-generated method stub
        Boolean b = false;
        GraphSession graph = WfContextBuilder.getJbpmContext().getGraphSession();
        JbpmContext ctx = WfContextBuilder.getJbpmContext().getJbpmContext();
        // List<TaskInstance> tis = ctx.getTaskList();

        // Get latest processInstance that references a certain document
        // Query qpi = WfDao
        // .getInstance()
        // .getSessao()
        // .createQuery(
        // "select max(vi.processInstance) from org.jbpm.context.exe.variableinstance.StringInstance as vi, vi.processInstance pi"
        // +
        // " where pi.end is null and vi.name like 'doc_%' and vi.value = :codigoDocumento");
        Query qpi = WfDao.getInstance().getSessao().createQuery(
                "select max(vi.processInstance.id) from org.jbpm.context.exe.variableinstance.StringInstance as vi inner join vi.processInstance as pi"
                        + " where pi.end is null and vi.name like 'doc_%' and vi.value = :codigoDocumento");
        qpi.setParameter("codigoDocumento", codigoDocumento);

        Long pid = (Long) qpi.uniqueResult();

        if (pid == null)
            return null;

        Query qvi = WfDao.getInstance().getSessao()
                .createQuery("select vi from org.jbpm.context.exe.variableinstance.StringInstance as vi"
                        + " where vi.processInstance.id = :pid and vi.name = :nomeDaVariavel");
        qvi.setParameter("pid", pid);
        qvi.setParameter("nomeDaVariavel", nomeDaVariavel);
        List<VariableInstance> vis = qvi.list();

        for (VariableInstance vi : vis) {
            if (vi.getValue() != null)
                return vi.getValue();
        }
        return null;
    } catch (Exception e) {
        if (!isHideStackTrace())
            e.printStackTrace(System.out);
        throw e;
    }
}

From source file:br.pro.x87.dao.MaquinaDao.java

public Maquina findMaquinaById(int id) {
    Session session = null;//ww w .j a  va2  s  .  co  m
    try {
        session = HibernateConnector.getInstance().getSession();
        Query query = session.createQuery("from Maquina s where s.id = :id");
        query.setParameter("id", id);

        List queryList = query.list();
        if (queryList != null && queryList.isEmpty()) {
            return null;
        } else {
            return (Maquina) queryList.get(0);
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        session.close();
    }
}