Group by month with criteria in Hibernate

后端 未结 2 510
逝去的感伤
逝去的感伤 2020-12-13 09:29

I\'m trying to get a report using Criteria and ProjectionList, and I\'m pretty new using this through hibernate. So I have this model:

private Long _userId;         


        
2条回答
  •  生来不讨喜
    2020-12-13 10:17

    You can use an SQLQuery in hibernate, here is an example:

     public List groupByMonth(ChartRequest request){
    
       SQLQuery query = getSession().createSQLQuery("SELECT \n" +
            "sum(this_.amount) as amount, \n" +
            "extract(month from this_.fecha) as month, \n" +
            "extract(year from this_.fecha) as  year\n" +
            "FROM jornada this_ \n" +
            "GROUP BY \n" +
            "month, \n" +
            "year \n" +
            "ORDER BY \n" +
            "year asc, \n" +
            "month asc");
    
          query.setResultTransformer(Transformers.aliasToBean(MonthlyPoint.class));
          return query.list();
    }
    
    
    public class MonthlyPoint {
        private Double year;
        private Double month;
        private Double amount;
      //-- getters and setters here --
    }
    

提交回复
热议问题