一个返回汇集值(aggregate values)的查询能够按照一个返回的类或组件(components)中的任何属性(property)进行分组:html
select cat.color, sum(cat.weight), count(cat) from Cat cat group by cat.color
select foo.id, avg(name), max(name) from Foo foo join foo.names name group by foo.id
having
子句在这里也容许使用.java
select cat.color, sum(cat.weight), count(cat) from Cat cat group by cat.color having cat.color in (eg.Color.TABBY, eg.Color.BLACK)
若是底层的数据库支持的话(例如不能在MySQL中使用),SQL的通常函数与汇集函数也能够出现 在having
与order by
子句中。数据库
select cat from Cat cat join cat.kittens kitten group by cat.id, cat.name, cat.other, cat.properties having avg(kitten.weight) > 100 order by count(kitten) asc, sum(kitten.weight) desc
注意group by
子句与 order by
子句中都不能包含算术表达式(arithmetic expressions). 也要注意Hibernate目前不会扩展group的实体,所以你不能写group by cat
,除非cat
的全部属性都不是汇集的(non-aggregated)。你必须明确的列出全部的非汇集属性。express
http://doc.javanb.com/hibernate-reference-3-2-0-zh/ch14s11.htmlide