Monday, August 11, 2014

Hibernate | 'unexpected AST node' - Suming boolean column in HQL

Today I've encountered this error developing Grails application that executes HQL (Hibernate Query Language) code:

unexpected AST node

HQL Query is supposed to pull all patients in  database that are marked as active, but have all of their treatments marked as inactive:


FROM PatientTreatment PT 
JOIN PT.patient P 
GROUP BY P.id  
HAVING SUM(case when PT.isActive then 1 else 0 end) = 0 
AND P.isActive=true 

However, after initial run - i got error mentioned above 'unexpected AST node' - which  implies there is something wrong with query syntax (unexpected abstract tree node).  It was obvious that problem is within sum() function, and after couple of minutes of research I've realized that HQL can't automatically consider 'PT.isActive' as expression, but rather this property's value MUST be explicitly given. So, HQL that does work is listed below:


FROM PatientTreatment PT 
JOIN PT.patient P 
GROUP BY P.id  
HAVING SUM(case when PT.isActive = true then 1 else 0 end) = 0 
AND P.isActive=true 


Hope this saves someone's time.

No comments:

Post a Comment