2012-01-20 70 views
1

是否可以在Hibernate HQL中構建以下SQL語句?HQL中的SQL計數HQL

SELECT COUNT(*) FROM table; 
+3

不僅僅是谷歌 「SELECT COUNT HQL」 :) –

+3

@SavinoSguera現在這裏谷歌搜索的,因爲你這個驅動,:) – Gab

回答

6

當然是:

select count(e) from Entity e; 

這樣的答案都在reference manual很容易瑪麗薩。

-2

是的,你可以使用SELECT COUNT(*)FROM表名;

1

使用聚合函數

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);