0

I have a HQL query that I'm trying to execute, but I keep getting a QueryException.

My query is

SELECT a FROM (SELECT MAX(m.version) maxVersionOp, m.codoperacion codOp FROM ConsumoMe m WHERE m.codoperacion  IN ('MEL101','MEL102') AND m.horizontetemporal IN('PM') GROUP BY m.codoperacion), ConsumoMe a WHERE codOp = a.codoperacion AND maxVersionOp = a.version AND a.fechaBajaLogica IS NULL

And the exception I get:

org.hibernate.QueryException: in expected: SELECT [SELECT a FROM (SELECT MAX(m.version) maxVersionOp, m.codoperacion codOp FROM es.enagas.siomprog.entities.ConsumoMe m WHERE m.codoperacion  IN ('MEL101','MEL102') AND m.horizontetemporal IN('PM') GROUP BY m.codoperacion), es.enagas.siomprog.entities.ConsumoMe a WHERE codOp = a.codoperacion AND maxVersionOp = a.version AND a.fechaBajaLogica IS NULL]

If I cut the inner select and try executing it:

SELECT MAX(m.version) maxVersionOp, m.codoperacion codOp FROM ConsumoMe m WHERE m.horizontetemporal IN('PM') AND (m.codoperacion = 'MEL101' OR m.codoperacion = 'MEL102') GROUP BY m.codoperacion

Then I get a similar exception:

org.hibernate.QueryException: , expected in SELECT [SELECT MAX(m.version) maxVersionOp, m.codoperacion codOp FROM es.enagas.siomprog.entities.ConsumoMe m WHERE m.horizontetemporal IN('PM') AND (m.codoperacion = 'MEL101' OR m.codoperacion = 'MEL102') GROUP BY m.codoperacion]

If I run the SQL equivalent queries, I get valid results. Furthermore, those error messages ", expected in SELECT" are not very helpful. Any idea of what could be the problem?

1
  • 2
    XYZ expected means that the HQL parser was expecting XYZ but got something else. It was expecting 'in' in the first query and ',' in the 2nd one Commented Jun 27, 2013 at 16:42

2 Answers 2

1

I see multiple errors in the SQL

m.codoperacion codOp --> m.codoperacion as codOp

AND m.horizontetemporal IN('PM') AND GROUP BY m.codoperacion --> there should not be AND

AND m.horizontetemporal IN('PM') GROUP BY m.codoperacion
Sign up to request clarification or add additional context in comments.

2 Comments

also MAX(m.version) maxVersionOp --> MAX(m.version) AS maxVersionOp
Thanks, "AND GROUP" was wrong, I pasted the wrong version of the query. About adding "as", it's not necessary, can be omitted. But still, removing that AND does not solve the problem.
0

The answer is in:

http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/queryhql.html#queryhql-subqueries

Hibernate doesn't allow subqueries in the from clause.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.