用hibernate和其他字段在一起进行计数 [英] count with other fields in group by with hibernate
问题描述
我想用hibernate返回组中一些其他字段的 count 行,并且我的实体类中没有任何表示count的字段。例如我有一个Payment实体类:
class Payment
{
private Long id;
private String totalCode;
private String activityCode;
私人长期金额;
//其他字段和setter / getters
}
sql查询:
$ p $ select count(*),sum(p.amount),p.total_code,p.activity_code
from tb_payment p
group by p.total_code,p.activity_code
和我的hibernate标准:
Session session = getCurrentSession();
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.groupProperty(totalCode))
.add(Projections.groupProperty(activityCode))
.add(Projections.sum(amount))
.add(Projections.count(id));
Criteria criteria = session.createCriteria(Payment.class);
criteria.setProjection(projectionList);
列表<付款> payments = criteria.list();
正如我所说,我的问题是我不知道在哪里/如何获取值count(from criteria.list())!?
我想这里是你的代码的正确版本:
Session session = getCurrentSession();
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.groupProperty(totalCode))
.add(Projections.groupProperty(activityCode))
.add(Projections.sum(amount))
.add(Projections.rowCount());
Criteria criteria = session.createCriteria(Payment.class);
criteria.setProjection(projectionList);
List< Object []> payments = criteria.list();
for(Object [] payment:payments){
System.out.println(totalCode:+ payment [0]);
System.out.println(activityCode:+ payment [1]);
System.out.println(amountSum:+ payment [2]);
System.out.println(rowCount:+ payment [3]);
}
I want to return count of rows with some other fields in a group by with hibernate, and i have not any field in my entity class representing count. for example i have a Payment entity class:
class Payment
{
private Long id;
private String totalCode;
private String activityCode;
private Long amount;
// other fields and setter/getters
}
sql query:
select count(*), sum(p.amount), p.total_code, p.activity_code
from tb_payment p
group by p.total_code,p.activity_code
and my hibernate criteria:
Session session = getCurrentSession();
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.groupProperty("totalCode"))
.add(Projections.groupProperty("activityCode"))
.add(Projections.sum("amount"))
.add(Projections.count("id"));
Criteria criteria = session.createCriteria(Payment.class);
criteria.setProjection(projectionList);
List<Payment> payments = criteria.list();
As i said, my problem is i don't know where/how can i access the value of count (from criteria.list())!?
I think here is the correct version of your code:
Session session = getCurrentSession();
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.groupProperty("totalCode"))
.add(Projections.groupProperty("activityCode"))
.add(Projections.sum("amount"))
.add(Projections.rowCount());
Criteria criteria = session.createCriteria(Payment.class);
criteria.setProjection(projectionList);
List<Object[]> payments = criteria.list();
for (Object[] payment : payments) {
System.out.println("totalCode: " + payment[0]);
System.out.println("activityCode: " + payment[1]);
System.out.println("amountSum: " + payment[2]);
System.out.println("rowCount: " + payment[3]);
}
这篇关于用hibernate和其他字段在一起进行计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!