在HQL上使用Min,Max和Count

时间:2008-12-01 13:56:23

标签: nhibernate hibernate hql min

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

像:

select min(p.age) from person p

由于

3 个答案:

答案 0 :(得分:12)

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

请参阅Hibernate Doc。中的aggregate functions

答案 1 :(得分:5)

这就是我在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;

    }

答案 2 :(得分:2)

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