我是HQL的新手,我正在尝试这样做:

select count(T) from (
    select inscription, max(wrd_step) as max
    from table aa
    where rd_context = ?
    group by inscription
) as T
where T.max = ?


错误是:

Caused by: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near line 1, column 21 [select count(T) from( select inscription, max(wrd_step) from table aa where rd_context = ? group by inscription) as T where T.max = ?]


谢谢

编辑:

HQL中的查询为:

SELECT count(distinct inscription)
FROM Entity
WHERE inscription in (
    select distinct inscription
    from Entity
    where rd_context = ?
    group by inscription
    having max(wrd_step) = ?
)

最佳答案

Hibernate文档指出:


  请注意,HQL子查询只能在select或where子句中发生。


http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#queryhql-subqueries

但是假设table是一个映射的实体(是吗?),您可以这样做(未经测试):

select count(aa)
from table aa
where rd_context = :param1
group by inscription
having max(wrd_step) = :param2

09-27 02:08