Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Criteria Query with Group By and aliasToBean

Tags:

java

hibernate

I have table /entity called SaleRecord with fields such as

@Entity
public class SaleRecord {
   private Long id;
   private String type;
   private Double amount;
   //Getter and Setter and more fields
}

I want to write below query using Criteria

SELECT s.type AS accountName, SUM(s.amount) AS amount 
FROM salerecord s
GROUP BY s.type

I have written using plain SQL in Hibernate as (Its working)

String sql = " SELECT s.type AS accountName, SUM(s.amount) AS amount ";
            sql += " FROM salerecord s ";
            sql += " GROUP BY s.type ";

List<CollectionDO> incomeList = (List<CollectionDO>) getSession().createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(CollectionDO.class)).list();

CollectionDO is another POJO class in which I want to populate the result.

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

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<CollectionDO>) criteria.list();

CollectionDO.java

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;
    }     
}
like image 438
Rahul Agrawal Avatar asked Sep 22 '12 11:09

Rahul Agrawal


People also ask

How do I use group by in criteria builder?

The GROUP BY clause is used to collect data from one or more tables and arrange them in a group. In Criteria API, the groupBy() method of AbstractQuery interface is used to filter the records and group them.

Can we use group by in HQL?

On this page we will provide Hibernate HQL Order By and Group By example. Order by orders the data on the basis of given property in ascending or descending order. Group by groups the data on the basis of given property. In HQL we perform order by and group by for the given property of entity or associated entities.


1 Answers

I think it is not able to Transform. as in Criteria column name is "type" but CollectionDO.java has field as "accountName"

Try it as follows (using this version of add to specify the alias name):

Criteria criteria = 
    getSession()
        .createCriteria(SaleRecord.class)
        .add(Restrictions.between("date", 
                                  reportForm.getFromDate(), 
                                  reportForm.getToDate()));

        .setProjection(Projections.projectionList()
            .add(Projections.property("type"), "accountName")
            .add(Projections.sum("amount"))
            .add(Projections.groupProperty("type")));
        .setResultTransformer(Transformers.aliasToBean(CollectionDO.class))
return (List<CollectionDO>) criteria.list();
like image 111
Vikdor Avatar answered Nov 15 '22 11:11

Vikdor