title: How to Create a Criteria Query with SUM and GROUP BY JPA Java
body: <blockquote>
    <p>Here my changes base on Ish Answer:</p>
  </blockquote>
  
  <pre><code>public List&lt;TopUpRAMSInfo&gt; listadoTOPUP2(Date fechaI, Date fechaF) {
          List&lt;TopUpRAMSInfo&gt; lista;
          CriteriaQuery&lt;TopUpRAMSInfo&gt; data = cb.createQuery(TopUpRAMSInfo.class);
          Root&lt;TopUpRAMS&gt; c = data.from(TopUpRAMS.class);
          data.select(cb.construct(TopUpRAMSInfo.class,
                  c.get("deviceId"),
                  cb.sum(c.get("rechargeValue"))
          )
          );
          data.where(cb.greaterThanOrEqualTo(c.get("connectionTime"), fechaI),
                  cb.lessThanOrEqualTo(c.get("connectionTime"), fechaF)
          );
          data.groupBy(c.get("deviceId"));
          TypedQuery&lt;TopUpRAMSInfo&gt; datos = emDAO.createQuery(data);
          lista = datos.getResultList();
          return lista;
      }
  </code></pre>
  
  <blockquote>
    <p>Now im having this error: 
    java.lang.IllegalArgumentException: An exception occured looking on class: class ENTITY.TopUpRAMSInfo for constructor using selection criteria types as arguments.  If this CriteriaQuery was not intended to be a constructor query please verify that the selection matches the return type.</p>
    
    <p>----- ORIGINAL POST -----</p>
  </blockquote>
  
  <p>I'm having some issues doing this. This is my first real program I'm trying to build, and I need to summarize one field based on the other.</p>
  
  <p>Here is my code so far (note that <code>TopUpRAMS</code> is my entity and <code>emDAO</code> is my Entity Manager.</p>
  
  <pre class="lang-java prettyprint-override"><code>public List&lt;TopUpRAMS&gt; listadoTOPUP2(Date fechaI, Date fechaF) {
      List&lt;TopUpRAMS&gt; lista;
  
      CriteriaQuery&lt;TopUpRAMS&gt; data = cb.createQuery(TopUpRAMS.class);
      Root&lt;TopUpRAMS&gt; c = data.from(TopUpRAMS.class);
  
      data.multiselect(c.get("deviceId"), cb.sum(c.get("rechargeValue")));
      data.where(cb.greaterThanOrEqualTo(c.get("connectionTime"), fechaI));
      cb.and(cb.lessThanOrEqualTo(c.get("connectionTime"), fechaF));
      data.groupBy(c.get("deviceId"));
  
      TypedQuery&lt;TopUpRAMS&gt; datos = emDAO.createQuery(data);
      lista = datos.getResultList();
      return lista;
  }
  </code></pre>