转换查询时出现hibernate.QueryException

时间:2013-06-27 16:31:54

标签: java sql hibernate hql

我有一个我正在尝试执行的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中预期”并不是很有帮助。 知道可能出现什么问题吗?

2 个答案:

答案 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)