Hibernate Group by Criteria Object

前端 未结 4 720
一整个雨季
一整个雨季 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:15

    You can use the approach @Ken Chan mentions, and add a single line of code after that if you want a specific list of Objects, example:

        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"))           
                        ).setResultTransformer(Transformers.aliasToBean(SomeClazz.class));
    
    List<SomeClazz> objectList = (List<SomeClazz>) criteria.list();
    
    0 讨论(0)
  • 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();
    
    0 讨论(0)
  • 2020-11-29 03:32

    GroupBy using in Hibernate

    This is the resulting code

    public Map getStateCounts(final Collection ids) {
        HibernateSession hibernateSession = new HibernateSession();
        Session session = hibernateSession.getSession();
        Criteria criteria = session.createCriteria(DownloadRequestEntity.class)
                .add(Restrictions.in("id", ids));
        ProjectionList projectionList = Projections.projectionList();
        projectionList.add(Projections.groupProperty("state"));
        projectionList.add(Projections.rowCount());
        criteria.setProjection(projectionList);
        List results = criteria.list();
        Map stateMap = new HashMap();
        for (Object[] obj : results) {
            DownloadState downloadState = (DownloadState) obj[0];
            stateMap.put(downloadState.getDescription().toLowerCase() (Integer) obj[1]);
        }
        hibernateSession.closeSession();
        return stateMap;
    }
    
    0 讨论(0)
  • 2020-11-29 03:33

    If you have to do group by using hibernate criteria use projections.groupPropery like the following,

    @Autowired
    private SessionFactory sessionFactory;
    Criteria crit = sessionFactory.getCurrentSession().createCriteria(studentModel.class);
    crit.setProjection(Projections.projectionList()
                .add(Projections.groupProperty("studentName").as("name"))
    List result = crit.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP).list(); 
    return result;  
    
    0 讨论(0)
提交回复
热议问题