Hibernate Group by Criteria Object

前端 未结 4 817
一整个雨季
一整个雨季 2020-11-29 02:42

I would like to implement the following SQL query with Hibernate Criteria:

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_n         


        
4条回答
  •  青春惊慌失措
    2020-11-29 03:16

    Please refer to this for the example .The main point is to use the groupProperty() , and the related aggregate functions provided by the Projections class.

    For example :

    SELECT column_name, max(column_name) , min (column_name) , count(column_name)
    FROM table_name
    WHERE column_name > xxxxx
    GROUP BY column_name
    

    Its equivalent criteria object is :

    List result = session.createCriteria(SomeTable.class)       
                        .add(Restrictions.ge("someColumn", xxxxx))      
                        .setProjection(Projections.projectionList()
                                .add(Projections.groupProperty("someColumn"))
                                .add(Projections.max("someColumn"))
                                .add(Projections.min("someColumn"))
                                .add(Projections.count("someColumn"))           
                        ).list();
    

提交回复
热议问题