【问题标题】:SQL Count in Hibernate HQLHibernate HQL 中的 SQL 计数
【发布时间】:2012-02-15 08:27:55
【问题描述】:

是否可以在 Hibernate HQL 中构建以下 SQL 语句?

SELECT COUNT(*) FROM table;

【问题讨论】:

  • 只需谷歌“选择计数 hql” :)
  • @SavinoSguera 现在在这里用谷歌搜索这个驱动器,因为你 :)

标签: java mysql sql hibernate


【解决方案1】:

当然是:

select count(e) from Entity e;

reference manual 中很容易找到这样的答案。

【讨论】:

    【解决方案2】:

    我没有可用于测试的休眠设置,但您似乎可以: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-aggregation

    【讨论】:

      【解决方案3】:

      使用聚合函数

      Query q = session
                  .createQuery("select count(*), max(eb.Eid) , min(eb.Eid) , sum(eb.Eid) , avg(eb.Eid) from Employee eb");
      
          List l = q.list();
      
          // Object result[]= (Object[])l.get(0);
          // for(Object i:result){
          // System.out.println(result[(int)i]);
          // }
      
          Object result[] = (Object[]) l.get(0);
      
          Long res1 = (Long) result[0];
          long count = res1.longValue();
      
          Integer res2 = (Integer) result[1];
          long max = res2.longValue();
      
          Integer res3 = (Integer) result[2];
          long min = res3.longValue();
      
          Long res4 = (Long) result[3];
          long sum = res4.longValue();
      
          Double res5 = (Double) result[4];
          double avg = res4.doubleValue();
      
          System.out.println("Number of records" + count);
          System.out.println("Maximum of  records" + max);
          System.out.println("Minimum of  records" + min);
          System.out.println("Sum of  records" + sum);
          System.out.println(" Average of  records" + avg);
      

      【讨论】:

        【解决方案4】:

        是的,您可以使用 SELECT COUNT(*) FROM table_name;

        【讨论】:

          猜你喜欢
          • 2020-10-04
          • 2011-05-08
          • 2019-10-31
          • 2016-02-28
          • 1970-01-01
          • 2017-01-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多