title: Best way to get aggregate function result inside the entity object
body: <p>Many time I have to get the SQL aggregate query result inside the Entity Object itself. As of now I could able to achive the same by the following code</p>
  
  <pre><code>    CriteriaBuilder cb = em.getCriteriaBuilder();
      CriteriaQuery&lt;Tuple&gt; q = cb.createTupleQuery();
      Root&lt;Test&gt; c = q.from(Test.class);
      q.multiselect(c, cb.count(c));
      q.groupBy(c.get("type"));
  
      TypedQuery&lt;Tuple&gt; t = em.createQuery(q);
      List&lt;Tuple&gt; resultList = t.getResultList();
      List&lt;Test&gt; list = new ArrayList&lt;&gt;();
  
      for(Tuple tuple : resultList){
          Test te = (Test) tuple.get(0);
          te.setQuantity((long)tuple.get(1));
          list.add(te);
      }
  </code></pre>
  
  <p>But I want to know what could be the best way. My Test Entity is as </p>
  
  <pre><code>@Entity
  @Table(name = "test")
  public class Test {
      @Id
      @Column(name = "id")
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Integer id;
  
      @Column(name = "name")
      private String name;
  
      @Column(name = "type")
      private Integer type = 0;
  
      @Transient
      private long quantity;
  }
  </code></pre>