我有一个我正在尝试执行的HQL查询,但是我一直收到QueryException。
我的查询是
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
我得到的例外:
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]
如果我剪切内部选择并尝试执行它:
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
然后我得到了一个类似的例外:
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]
如果我运行SQL等效查询,我会得到有效的结果。此外,那些错误消息“,在SELECT中预期”并不是很有帮助。 知道可能出现什么问题吗?
答案 0 :(得分:1)
我在SQL中看到了多个错误
m.codoperacion codOp
- > m.codoperacion as codOp
AND m.horizontetemporal IN('PM') AND GROUP BY m.codoperacion
- >不应该有AND
AND m.horizontetemporal IN('PM') GROUP BY m.codoperacion
答案 1 :(得分:0)
答案在:
http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/queryhql.html#queryhql-subqueries
Hibernate不允许from子句中的子查询。