【问题标题】:Using Min, Max and Count on HQL在 HQL 上使用 Min、Max 和 Count
【发布时间】:2010-09-24 18:14:50
【问题描述】:

hibernate HQL查询是否支持使用select min、max、count等sql函数?

喜欢:

select min(p.age) from person p

谢谢

【问题讨论】:

标签: nhibernate hibernate hql min


【解决方案1】:

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

请参阅 Hibernate 文档中的 aggregate functions

【讨论】:

  • 我不得不否决这个答案,因为它很快就会被弃用。答案给人的印象是只有这三个功能存在,而其他功能也很多
【解决方案2】:

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

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

    【讨论】:

    • 不幸的是,链接已更改。你能重新检索页面吗?谢谢!
    • 感谢您的提醒。链接固定
    猜你喜欢
    • 2019-03-21
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    • 1970-01-01
    相关资源
    最近更新 更多