【问题标题】:HQL Query not able to sort by distinctHQL 查询无法按不同排序
【发布时间】:2018-01-15 11:33:58
【问题描述】:

我有一个 HQL 查询,当我执行一个简单的 distinct 时运行得很好:

select distinct type from ACLIENT where showing = 1

但是现在我想对结果进行排序,所以我尝试了这个:

select distinct type as btype from ACLIENT where showing = 1 order by btype

但这不起作用,我得到一个 SQLGrammarException。 谁能建议我犯了什么错误以及如何按不同类型对结果进行排序?

我使用的 Hibernate 版本是 3.6,完整的堆栈跟踪是:

Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.loader.Loader.doList(Loader.java:2235)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
    at org.hibernate.loader.Loader.list(Loader.java:2124)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1149)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:67)
    ... 126 more
Caused by: org.postgresql.util.PSQLException: ERROR: column "btype" does not exist
  Hint: Perhaps you meant to reference the column "aclient0_.type".
  Position: 204
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2161)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1890)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:560)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:417)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:302)
    at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:353)
    at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1812)
    at org.hibernate.loader.Loader.doQuery(Loader.java:697)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
    at org.hibernate.loader.Loader.doList(Loader.java:2232)

【问题讨论】:

  • 您应该编辑您的帖子并添加完整的错误消息/堆栈跟踪,因为他们(可能)提到了哪个令牌导致了问题另外hibernate tools for hql 可能会有所帮助
  • 它无法识别不同类型的别名和堆栈跟踪显示:"Caused by: org.postgresql.util.PSQLException: ERROR: column "btype" does not exist 提示:也许你的意思是参考“aclient0_.type”列。位置:204“
  • ORDER BY 表达式必须出现在您正在使用别名使用类型的选择列表中
  • 请发布 ACLIENT 类正文

标签: hibernate hql


【解决方案1】:

这似乎是 Hibernate 3.6 中 order by 子句中别名的错误。如此处报告:https://hibernate.atlassian.net/browse/HHH-892 根据链接的解决方案或解决方法是:

select distinct type as btype from AirsClient where showing = 1 order by col_0_0_

也就是说,我使用 col_0_0 而不是别名,它可以工作。

【讨论】:

  • 这不起作用吗? select distinct type as btype from ACLIENT where showing = 1 order by type
猜你喜欢
  • 2010-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-02
  • 2011-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多