Search code examples
javahibernatehql

HQL SUM Query - unexpected token


I can execute an HQL as follows:

select order.anticipatedMonetaryTotal.payableAmount.value from OrderType order

using a code like:

EntityManager e = this.entityManagerFactory.createEntityManager();
e.getTransaction().begin();
result = e.createQuery(query).getResultList();

So, I know that the order.anticipatedMonetaryTotal.payableAmount.value is a correct location to fetch values (a list of BigDecimal values).

However, I'm getting a:

line 1:14: unexpected token: order
antlr.NoViableAltException: unexpected token: order

for the following query:

select SUM ( order.anticipatedMonetaryTotal.payableAmount.value) from OrderType order

So, what do I miss about the SUM notation? As far as I can see at https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-aggregation, it is quite straightforward and I cannot see any mistake in my case.


Solution

  • This is not because of SUM function, this is because of order word. This is reserved word which cannot be used for variables. Change it on something other e.g. orderType

    select SUM ( orderType.anticipatedMonetaryTotal.payableAmount.value) from OrderType orderType
    

    This should solve your issue, because everything else is fine.

    Good luck.