Group by month with criteria in Hibernate

后端 未结 2 507
逝去的感伤
逝去的感伤 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 09:57

    I found the answer, and its pretty simple. I changed the "groupProperty" in the ProjectionList criteria for this:

    projectionList.add(Projections.sqlGroupProjection(
        "month({alias}.DATE) as month, year({alias}.DATE) as year", 
        "month({alias}.DATE), year({alias}.DATE)", 
        new String[]{"month","year"}, 
        new Type[] {Hibernate.DOUBLE}));
    

    Okay. I'll explain the sqlGroupProjection. The first argument is the part of the query after of the "select", for example:

    Select [firstPartOfSqlGroupProjection] * boo;
    

    The "{alias}" is the alias that hibernates use in the query to refer to a table.

    The second argument of the sqlGroupProjection function is the group by criteria, the third argument is the column names that you'll get from the group by and finally, the type of data you'll use.

提交回复
热议问题