在HQL上使用Min,Max和Count

Dan*_*xis 6 nhibernate hibernate hql min

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

喜欢:

select min(p.age) from person p

谢谢

Gal*_*ian 12

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

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


Sah*_*wal 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;

    }
Run Code Online (Sandbox Code Playgroud)