title: Hibernate Criteria Query with Group By and aliasToBean
body: <p>I have table /entity called SaleRecord with fields such as</p>
  
  <pre><code>@Entity
  public class SaleRecord {
     private Long id;
     private String type;
     private Double amount;
     //Getter and Setter and more fields
  }
  </code></pre>
  
  <p>I want to write below query using Criteria</p>
  
  <pre><code>SELECT s.type AS accountName, SUM(s.amount) AS amount 
  FROM salerecord s
  GROUP BY s.type
  </code></pre>
  
  <p>I have written using plain SQL in Hibernate as (Its working)</p>
  
  <pre><code>String sql = " SELECT s.type AS accountName, SUM(s.amount) AS amount ";
              sql += " FROM salerecord s ";
              sql += " GROUP BY s.type ";
  
  List&lt;CollectionDO&gt; incomeList = (List&lt;CollectionDO&gt;) getSession().createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(CollectionDO.class)).list();
  </code></pre>
  
  <p>CollectionDO is another POJO class in which I want to populate the result.</p>
  
  <p>But want to write using criteria, So how to write this query and transform result into CollectionDO class.
  I have tried following but not working</p>
  
  <pre><code>Criteria criteria = getSession().createCriteria(SaleRecord.class).setResultTransformer(Transformers.aliasToBean(CollectionDO.class));
          criteria.setProjection(Projections.property("type"));
          criteria.setProjection(Projections.sum("amount"));        
          criteria.setProjection(Projections.groupProperty("type"));
          return (List&lt;CollectionDO&gt;) criteria.list();
  </code></pre>
  
  <p><strong>CollectionDO.java</strong></p>
  
  <pre><code>public class CollectionDO {    
      private Double amount;    
      private String accountName;
  
      public String getAccountName() {
          return accountName;
      }
      public void setAccountName(String accountName) {
          this.accountName = accountName;
      }
      public Double getAmount() {
          return amount;
      }
      public void setAmount(Double amount) {
          this.amount = amount;
      }     
  }
  </code></pre>