HQL queries using -in- frequently cause HQL syntax errors through Tomcat
Many HQL queries which use the HQL
inoperation (element of set or list), can cause a syntactically incorrect HQL query. Syntax error are then reported through Tomcat. An example query is:
from Issue as i where ~u in i._project._members
In whichSubmitted by Sander Vermolen on 9 February 2010 at 14:25
Issueis an entity,
i._project._membersrefers to a set of users.
I found a work-around for this issue, that does not use inefficient code. A query using
incan be rewritten to a query using two joins and a select. For example, the above query can be rewritten to:
select i from Issue as i left join i._project._members as m where m = ~u
Note that separating the joins, for example to add a condition on the project attributes, is not supported in the WebDSL syntax.
The problem here seems to be pretty printing of HQL queries before they are passed to Hibernate? Is it a lack of parenthization or are the pretty printer rules b0rked?
You can also use the elements() function like this:
from Issue as i
where ~u in elements(i._project._members)
This will generate a subquery fetching the ids from the join table. You can also use indices() instead to select the indices from the join table, which may be of use in other situations.
You also get exceptions for queries like:
from Issue as i
where i._project in (~projects)
In this case I got org.hibernate.PropertyAccessException and java.lang.IllegalArgumentException. The problem here is that WebDSL uses setParameter() instead of setParameterList() to bind the variable “projects”, which is a list of project objects. I have not found a way around this issue.
Log in to post comments