0

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

Cork Kochi
  • 1,783
  • 6
  • 29
  • 45

1 Answers1

0

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.

Siva Kumar
  • 1,983
  • 3
  • 14
  • 26