java 带有 Group By 和 aliasToBean 的 Hibernate Criteria 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12543512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:18:56  来源:igfitidea点击:

Hibernate Criteria Query with Group By and aliasToBean

javahibernate

提问by Rahul Agrawal

I have table /entity called SaleRecord with fields such as

我有名为 SaleRecord 的表/实体,其中包含诸如

@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

我想使用 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)

我在 Hibernate 中使用纯 SQL 编写为(它的工作原理)

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.

CollectionDO 是另一个 POJO 类,我想在其中填充结果。

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

但是想写使用条件,那么如何编写这个查询并将结果转换为CollectionDO类。我试过跟随但没有工作

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

集合DO.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;
    }     
}

回答by Vikdor

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

我认为它无法转换。如标准列名称为“类型”,但 CollectionDO.java 的字段为“accountName”

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

尝试如下(使用这个版本的add来指定别名):

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();