Having trouble with Hibernate HQL query containing nested aggregate functions

StackOverflow https://stackoverflow.com/questions/22203277

  •  08-06-2023
  •  | 
  •  

Question

The following hql_query String works and delivers data records as expected. It gives me a list of lists, where the nested lists consist of a person pers and "the amount of 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='UID_Department'"
+ " and persAttr.wert='Sales (10100200)'"
+ " group by pers";

Now, what i want to achieve and fail with is to get the average value of all anzBo's. I tried the following query_string:

query_string = ""
+ "select avg(anzBo) from "
+ " (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='UID_Department'"
+ " and persAttr.wert='Sales (10100200)'"
+ " group by pers)";

I get the error message:

unexpected token: select near line 1, column 25 [select avg(anzBo) from select distinct pers ...

I hope you got the clue what im trying to do. Can you help me to form the correct hql_string?

UPDATE:

If i try this:

query_string = ""
+ "select distinct pers, count(persAccsBOs) as anzBo, avg(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='UID_Department'"
+ " and persAttr.wert='Sales (10100200)'"
+ " group by pers";

i will get a java.lang.NullPointerException.

Pas de solution correcte

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top