Using Min, Max and Count on HQL

ⅰ亾dé卋堺 提交于 2019-12-10 13:32:32

问题


Does hibernate HQL queries support using select min, max, count and other sql functions?

like:

select min(p.age) from person p

Thanks


回答1:


Yes, min(), max() and count() are supported in HQL.

see aggregate functions in the Hibernate Doc.




回答2:


thats how I am using max in Hibernate :

public long getNextId(){
long appId;         
try{
            Session session = HibernateUtil.getAdmSessionFactory().getCurrentSession();
            Transaction t = session.beginTransaction();
            String sequel = "Select max(JAdmAppExemptionId) from JAdmAppExemption";
            Query q = session.createQuery(sequel);
            List currentSeq = q.list();
            if(currentSeq == null){
                return appId;
            }else{
            appId = (Long)currentSeq.get(0);
            return appId+1;
            }

        }catch(Exception exc){
            System.out.print("Unable to get latestID");
            exc.printStackTrace();

        }
        return 0;

    }



回答3:


Some aggregate functions are supported: look in the manual



来源:https://stackoverflow.com/questions/330918/using-min-max-and-count-on-hql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!