Hibernate:如何只获取计数值大于阈值的结果对象

时间:2014-03-06 14:08:15

标签: hibernate hql

这是我的query_string。它提供包括计数在内的所有人(persAccsBOs)。这个字符串有效。

hql_query_string = ""
+ "select distinct pers, count(persAccsBOs) as anzBo from Person as pers"
+ " inner join pers.personenAttribute as persAttr inner join persAttr.pa_pk.attribut as pAttr"
+ " inner join pers.accounts as persAccs"
+ " inner join persAccs.berechtigungsobjekte as persAccsBOs"
+ " where pAttr.name= :attrName"
+ " and persAttr.wert= :attrValue"
+ " group by pers";

现在我想只获得计数(persAccsBOs)大于阈值的人。这不起作用:

hql_query_string = ""
+ "select distinct pers, count(persAccsBOs) as anzBo from Person as pers"
+ " inner join pers.personenAttribute as persAttr inner join persAttr.pa_pk.attribut as pAttr"
+ " inner join pers.accounts as persAccs"
+ " inner join persAccs.berechtigungsobjekte as persAccsBOs"
+ " where pAttr.name= :attrName"
+ " and persAttr.wert= :attrValue"
+ " and count(persAccsBOs) >=  :threshold"
+ " group by pers";

我收到错误消息:

  

14:55:27.121 [main] DEBUG   org.hibernate.engine.jdbc.spi.SqlExceptionHelper 139 logExceptions -   无法提取ResultSet [n / a] java.sql.SQLException:无效使用   群体功能

你能帮帮我吗?

1 个答案:

答案 0 :(得分:3)

亚历山大,

我相信这会奏效:

hql_query_string = ""
+ "select distinct pers, count(persAccsBOs) as anzBo from Person as pers"
+ " inner join pers.personenAttribute as persAttr inner join persAttr.pa_pk.attribut as pAttr"
+ " inner join pers.accounts as persAccs"
+ " inner join persAccs.berechtigungsobjekte as persAccsBOs"
+ " where pAttr.name= :attrName"
+ " and persAttr.wert= :attrValue"
+ " group by pers"
+ " having count(persAccsBOs) >=  :threshold";

聚合字段的条件必须使用having子句,而不是where子句。