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:br.com.hslife.orcamento.repository.CategoriaRepository.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Categoria> findTipoCategoriaAndDescricaoAndAtivoByUsuario(TipoCategoria tipoCategoria,
        String descricao, Boolean ativo, List<Usuario> usuarios) {
    StringBuilder hql = new StringBuilder();
    hql.append("FROM Categoria categoria WHERE ");
    if (tipoCategoria != null) {
        hql.append("categoria.tipoCategoria = :tipo AND ");
    }/*from   w w w  .j  a v a  2s  .  c o m*/
    if (descricao != null && !descricao.isEmpty()) {
        hql.append("categoria.descricao LIKE '%");
        hql.append(descricao);
        hql.append("%' AND ");
    }
    if (ativo != null) {
        hql.append("categoria.ativo = :ativo AND ");
    }

    hql.append("categoria.usuario.id IN (:idUsuario) ORDER BY categoria.descricao ASC");

    Query hqlQuery = getQuery(hql.toString());
    if (tipoCategoria != null) {
        hqlQuery.setParameter("tipo", tipoCategoria);
    }
    if (ativo != null) {
        hqlQuery.setParameter("ativo", ativo);
    }

    // Resgata os IDs dos usurios
    List<Long> idsUsuarios = new ArrayList<>();
    for (Usuario u : usuarios) {
        if (u != null && u.getId() != null) {
            idsUsuarios.add(u.getId());
        }
    }

    hqlQuery.setParameterList("idUsuario", idsUsuarios);

    return hqlQuery.list();
}

From source file:br.com.hslife.orcamento.repository.ContaRepository.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Conta> findDescricaoOrTipoContaOrAtivoByUsuario(String descricao, TipoConta[] tipoConta,
        Usuario usuario, Boolean ativo) {
    StringBuilder hql = new StringBuilder();
    hql.append("FROM Conta conta WHERE ");
    if (descricao != null) {
        hql.append("conta.descricao LIKE '%");
        hql.append(descricao);// ww w  .j  av a  2s .c  o  m
        hql.append("%' AND ");
    }
    if (tipoConta != null && tipoConta.length != 0) {
        hql.append("conta.tipoConta IN (:tipo) AND ");
    }
    if (ativo != null) {
        hql.append("conta.ativo = :ativo AND ");
    }

    hql.append("conta.usuario.id = :idUsuario ORDER BY conta.descricao ASC");

    Query hqlQuery = getQuery(hql.toString());

    if (tipoConta != null && tipoConta.length != 0) {
        hqlQuery.setParameterList("tipo", tipoConta);
    }
    if (ativo != null) {
        hqlQuery.setBoolean("ativo", ativo);
    }

    hqlQuery.setLong("idUsuario", usuario.getId());

    return hqlQuery.list();
}

From source file:br.com.hslife.orcamento.repository.FavorecidoRepository.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Favorecido> findTipoPessoaAndNomeAndAtivoByUsuario(TipoPessoa tipoPessoa, String nome,
        Boolean ativo, List<Usuario> usuarios) {
    StringBuilder hql = new StringBuilder();
    hql.append("FROM Favorecido favorecido WHERE ");
    if (tipoPessoa != null) {
        hql.append("favorecido.tipoPessoa = :tipo AND ");
    }// w w  w  .  ja  v  a2  s. c om
    if (nome != null && !nome.isEmpty()) {
        hql.append("favorecido.nome LIKE '%");
        hql.append(nome);
        hql.append("%' AND ");
    }
    if (ativo != null) {
        hql.append("favorecido.ativo = :ativo AND ");
    }

    hql.append("favorecido.usuario.id IN (:idUsuario) ORDER BY favorecido.nome ASC");

    Query hqlQuery = getQuery(hql.toString());
    if (tipoPessoa != null) {
        hqlQuery.setParameter("tipo", tipoPessoa);
    }
    if (ativo != null) {
        hqlQuery.setParameter("ativo", ativo);
    }

    // Resgata os IDs dos usurios
    List<Long> idsUsuarios = new ArrayList<>();
    for (Usuario u : usuarios) {
        if (u != null && u.getId() != null) {
            idsUsuarios.add(u.getId());
        }
    }

    hqlQuery.setParameterList("idUsuario", idsUsuarios);

    return hqlQuery.list();
}

From source file:br.com.hslife.orcamento.repository.MeioPagamentoRepository.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<MeioPagamento> findDescricaoAndAtivoByUsuario(String descricao, Boolean ativo,
        List<Usuario> usuarios) {
    StringBuilder hql = new StringBuilder();
    hql.append("FROM MeioPagamento meioPagamento WHERE ");

    if (descricao != null && !descricao.isEmpty()) {
        hql.append("meioPagamento.descricao LIKE '%");
        hql.append(descricao);/*from   w  ww . j  a  v  a2s  .c  om*/
        hql.append("%' AND ");
    }
    if (ativo != null) {
        hql.append("meioPagamento.ativo = :ativo AND ");
    }

    hql.append("meioPagamento.usuario.id IN (:idUsuario) ORDER BY meioPagamento.descricao ASC");

    Query hqlQuery = getQuery(hql.toString());

    if (ativo != null) {
        hqlQuery.setParameter("ativo", ativo);
    }

    // Resgata os IDs dos usurios
    List<Long> idsUsuarios = new ArrayList<>();
    for (Usuario u : usuarios) {
        if (u != null && u.getId() != null) {
            idsUsuarios.add(u.getId());
        }
    }

    hqlQuery.setParameterList("idUsuario", idsUsuarios);

    return hqlQuery.list();
}

From source file:br.com.suricattus.surispring.framework.service.GenericRetrieveService.java

License:Open Source License

/**
 * Retorna a lista de entidades de acordo com a namedQuery informada.
 * @param <T>// w w w.  j  a v a2 s. c o m
 * @param classe
 * @param namedQuery
 * @param params
 * @return
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> List<T> retrieveByNamedQuery(Class<T> classe, String namedQuery, Map<String, Object> params) {
    Query query = getSession().getNamedQuery(namedQuery);
    if (params != null) {
        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 {
                query.setParameter(paramName, params.get(paramName));
            }
        }
    }
    return query.list();
}

From source file:br.com.suricattus.surispring.framework.service.GenericRetrieveService.java

License:Open Source License

/**
 * Retorna a entidade de acordo com a namedQuery informada.
 * @param <T>//  w  w w.j a v  a2  s  .co m
 * @param classe
 * @param namedQuery
 * @param params
 * @return
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> T retrieveUniqueByNamedQuery(Class<T> classe, String namedQuery, Map<String, Object> params) {
    Query query = getSession().getNamedQuery(namedQuery);
    if (params != null) {
        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 {
                query.setParameter(paramName, params.get(paramName));
            }
        }
    }
    return (T) query.uniqueResult();
}

From source file:br.com.suricattus.surispring.framework.service.GenericRetrieveService.java

License:Open Source License

/**
 * Retorna o resultado de acordo com a namedQuery informada.
 * @param classe/*from   w w  w  .j  a  va  2 s.  co m*/
 * @param namedQuery
 * @param params
 * @return
 */
@SuppressWarnings("rawtypes")
public List<?> retrieveByNamedQuery(String namedQuery, Map<String, Object> params) {
    Query query = getSession().getNamedQuery(namedQuery);
    if (params != null) {
        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 {
                query.setParameter(paramName, params.get(paramName));
            }
        }
    }
    return query.list();
}

From source file:br.com.suricattus.surispring.framework.service.GenericRetrieveService.java

License:Open Source License

/**
 * Retorna o resultado unico de acordo com a namedQuery informada.
 * @param classe/*from w  w  w.j  av a2 s  .  c  om*/
 * @param namedQuery
 * @param params
 * @return
 */
@SuppressWarnings("rawtypes")
public Object retrieveUniqueByNamedQuery(String namedQuery, Map<String, Object> params) {
    Query query = getSession().getNamedQuery(namedQuery);
    if (params != null) {
        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 {
                query.setParameter(paramName, params.get(paramName));
            }
        }
    }
    return query.uniqueResult();
}

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  ww w.j av  a  2 s.co  m
            query.setParameter(paramName, params.get(paramName));
        }
    }

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

    return query;
}

From source file:br.gov.jfrj.siga.hibernate.ExDao.java

License:Open Source License

public String consultarDescricaoExClassificacao(ExClassificacao exClass) {
    String[] pais = MascaraUtil.getInstance().getPais(exClass.getCodificacao());
    if (pais == null) {
        return exClass.getDescrClassificacao();
    }/*from w w w .j a  v a2s .  c om*/
    // pais = Arrays.copyOf(pais, pais.length+1);
    // pais[pais.length-1]= exClass.getCodificacao();

    Query q = getSessao().getNamedQuery("consultarDescricaoExClassificacao");
    q.setParameterList("listaCodificacao", pais);
    List<String> result = q.list();
    StringBuffer sb = new StringBuffer();
    for (String descr : result) {
        sb.append(descr);
        sb.append(": ");
    }

    sb.append(exClass.getDescrClassificacao());
    return sb.toString();
}