Search code examples
javahibernatehql

HQL Error in Named Query


I'm getting Hibernate's message that I have an "Error in named query".

Below is the HQL statement I'm trying to execute.

select sum(subtotal)
from (
      select sum(coalesce(sum(t2.amount), 0) - coalesce(sum(t3.amount), 0)) subtotal
      from submission t1
      left join t1.bills t2
      left join t1.payments t3
      where t1.id = 10
      group by t1.id

      union

      select sum(coalesce(sum(t2.amount), 0) - coalesce(sum(t3.amount), 0)) subtotal
      from submission t1
      left join t1.otherBills t2
      left join t1.payments t3
      where t1.id = 10
      group by t1.id
) a1

I think the problem might be related to the sum function in the outer query or the union operator but I haven't been able to narrow it down.

Does anyone have any ideas?


Solution

  • You can't use a subquery after From clause in HQL. My suggestion is that you can use native query. For more detail you should look at following link :- Hibernate Query Limitation