Hibernate Query Parsing Isssue with Concatenated Strings

I have code like the following:

String query = "from Foo f "

if (someChoice) {
query += "order by f.name"
} else {
query += "order by f.email"
}

Query dbQuery = session.createQuery(query);


IDEA gives me a red error at both "Order" keywords saying a "FROM" was expected. The parsing seems to be fairly complete except for maybe needing to look at the value of the String this String is being concatenated with. Has anyone else seen this issue?

9 comments
Comment actions Permalink

In my current project, we have literally hundreds of such HQL queries with
conditional parts, so a more intelligent parsing would be much appreciated.

Additionally, we actually use a static facade with generic "find" methods
taking HQL strings as arguments, and using varargs for "?" query parameters.
So, what I need to really be able to use the new HQL parsing is that IDEA
would provide some configurable list of methods whose parameters contain HQL
query strings and parameters.


Rogério Liesenfeld

"Patrick Burleson" <no_reply@jetbrains.com> wrote in message
news:12355778.1191702162571.JavaMail.itn@is.intellij.net...
>I have code like the following:
>

String query = "from Foo f "

>

if (someChoice) {
query += "order by f.name"
} else {
query += "order by f.email"
}

>

Query dbQuery = session.createQuery(query);

>
>

IDEA gives me a red error at both "Order" keywords saying a "FROM" was
expected. The parsing seems to be fairly complete except for maybe needing
to look at the value of the String this String is being concatenated with.
Has anyone else seen this issue?



0
Comment actions Permalink

In my current project, we have literally hundreds of such HQL queries
with conditional parts, so a more intelligent parsing would be much
appreciated.


Hmm, I would assume that such 'intelligent' parsing would be quite involved.
What if a concatenated substring is only a valid query chunk if an earlier
conditional concatenation has been performed?

You know, this kind of conditional query building is exactly the use case
for the Hibernate 'Criteria' API. (Also coming to JPA2 soon).
I think the best solution for now is to disable HQL/JPAQL validation when
some parts of the query are conditionally concatenated.

-tt


0
Comment actions Permalink

Hello Patrick,

I have code like the following:

String query = "from Foo f "

if (someChoice) {
query += "order by f.name"
} else {
query += "order by f.email"
}
Query dbQuery = session.createQuery(query);

IDEA gives me a red error at both "Order" keywords saying a "FROM" was
expected. The parsing seems to be fairly complete except for maybe
needing to look at the value of the String this String is being
concatenated with. Has anyone else seen this issue?


As far as I know, IntelliJ IDEA is unable to analyze concatenated queries
correctly if concatenations are not linear. As you can imagine, handling
any more complex cases requires much more complicated analysis.

--
Dmitry Jemerov
Software Developer
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Dmitry,

I appreciate the complexity of the issue. I guess now I'd just like a way to turn off the inspection. Right now I've got all the red lines in my project that really aren't issues. I've tried turning off Java EE and Hibernate inspections, but still now go. Is there another one I've missed?

Patrick

0
Comment actions Permalink

I have created a JIRA ticket:
http://www.jetbrains.net/jira/browse/IDEA-15533

Feel free to add comments and/or sample code.

-tt


0
Comment actions Permalink

Patrick Burleson wrote:

I have code like the following:

String query = "from Foo f "

if (someChoice) {
query += "order by f.name"
} else {
query += "order by f.email"
}

Query dbQuery = session.createQuery(query);


IDEA gives me a red error at both "Order" keywords saying a "FROM" was expected.


Not really an answer, but you really should be using Hibernates Criteria
API for doing this rather than building strings..

0
Comment actions Permalink

Not really an answer, but you really should be using Hibernates
Criteria API for doing this rather than building strings


In addition, IDEA (next version) could provide completion when working with
Hibernate Criteria API: IDEA-13154

-tt


0
Comment actions Permalink

Not really an answer, but you really should be using
Hibernates Criteria
API for doing this rather than building strings..


This is some legacy code I've been brought in to enhance. I'm contemplating converting it to Criteria API, but don't know if it's worth the time right now since the current code works.

0
Comment actions Permalink

Hello Patrick,

This is some legacy code I've been brought in to enhance. I'm
contemplating converting it to Criteria API, but don't know if it's
worth the time right now since the current code works.


That seems a waste of time.
Vote or comment on IDEA-15533 to get it fixed.


0

Please sign in to leave a comment.