在HQL上使用Min,Max和Count

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在HQL上使用Min,Max和Count相关的知识,希望对你有一定的参考价值。

hibernate HQL查询是否支持使用select min,max,count和其他sql函数?

喜欢:

select min(p.age) from person p

谢谢

答案

是的,HQL支持min()max()count()

请参阅Hibernate Doc中的aggregate functions

另一答案

多数民众赞成我如何在Hibernate中使用max:

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;

    }
另一答案

支持一些聚合函数:查看manual

以上是关于在HQL上使用Min,Max和Count的主要内容,如果未能解决你的问题,请参考以下文章