简体   繁体   中英

Group by month in Hibernate Criteria queries

I am new to Hibernate Criteria Queries , I am struggling to get the count based on month.

   session.createCriteria(Task.class)                                                                           
  .add(Restrictions.eq("completed", true))                     
  .add(Restrictions.in("site", sites))  
  .setProjection(Projections.rowCount()).uniqueResult();
  //group by "completedTime"    

If I have to get the count group By Month based on completedTime. How can i achieve this

Please try below code

    Criteria criteria = session.createCriteria(Task.class);
    criteria.add(Restrictions.eq("completed", true));                     
    criteria.add(Restrictions.in("site", sites));

    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.rowCount());
    projectionList.add(Projections.groupProperty("month"));

    criteria.setProjection(projectionList);
    criteria.addOrder(Order.asc("completedTime"));

You need to create projectionList to add projected column in it.

EDIT 1

projectionList.add(Projections.property("month"));

add month also in projectionList , so that you can able to retrieve the value.

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