简体   繁体   中英

How to use group by with sum in criteria

I have 3 columns in my table id, uid and value. I want to sum the values of each uid. I know the query but i want to use criteria.

Query:

 SELECT uid,SUM(value) FROM post GROUP BY uid;

How can i use this in hibernate criteria?

例如:

SELECT uid,SUM(value) FROM post GROUP BY uid HAVING SUM(value) > 100;

You can do it like this.

Criteria cr = session.createCriteria(Post.class); cr.setProjection(Projections.sum("value")); cr.setProjection(Projections.groupProperty("uid"));

amount and employee is column name to use using ProjectionsList:- 列名称:-
Criteria criteria=sessionFactory.getCurrentSession().createCriteria(EmpAdvance.class);
ProjectionList projectionList=Projections.projectionList();
projectionList.add(Projections.sum("amount"));
projectionList.add(Projections.groupProperty("employee"));
criteria.setProjection(projectionList);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM