How to get the count and objects with criteria jpa?

2

I need to return the collation results and also the number of each item in a query with criteria jpa.

public List<Dica> obterDicaPorUsuario(){
    final CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); 
    final CriteriaQuery<Dica> cQuery = cb.createQuery(Dica.class);
    final Root<Dica> root = cQuery.from(Dica.class);

    List<Predicate> condicoes = new ArrayList<Predicate>();

    Expression<Usuario> EpDescricao = root.get("usuario");

   cQuery.groupBy(root.get("usuario")); 
   cQuery.orderBy(cb.desc(cb.count(root.get("usuario"))));

    cQuery.select(root).where(condicoes.toArray(new Predicate[]{}));

    List<Dica> list = getEntityManager().createQuery(cQuery).getResultList();
    for (Dica dica : list) {
        System.out.println(" - -  " +dica.getUsuario().getNome() );

    } 

    return list;

}

This is perfectly grouping, but I need to also return how much of each grouping was done. If anyone knows a better way to do this, thank you.

    
asked by anonymous 13.02.2018 / 17:48

0 answers