在HQL上使用Min,Max和Count

时间:2021-10-22 22:52:30

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

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

like:

select min(p.age) from person p

从人p中选择min(p.age)

Thanks

3 个解决方案

#1


12  

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

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

see aggregate functions in the Hibernate Doc.

请参阅Hibernate Doc中的聚合函数。

#2


5  

thats how I am using max in Hibernate :

多数民众赞成我如何在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;

    }

#3


2  

Some aggregate functions are supported: look in the manual

支持某些聚合函数:查看手册

#1


12  

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

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

see aggregate functions in the Hibernate Doc.

请参阅Hibernate Doc中的聚合函数。

#2


5  

thats how I am using max in Hibernate :

多数民众赞成我如何在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;

    }

#3


2  

Some aggregate functions are supported: look in the manual

支持某些聚合函数:查看手册