0

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

4

2 回答 2

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
于 2013-06-27T16:41:28.527 回答
0

答案在:

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

Hibernate 不允许在 from 子句中使用子查询。

于 2013-07-03T15:44:46.033 回答