【问题标题】:What's wrong with this HQL? Where is the missing comma?这个 HQL 有什么问题?缺少的逗号在哪里?
【发布时间】:2014-08-08 09:37:54
【问题描述】:

我遇到了这个休眠异常:

 org.hibernate.QueryException: , expected in SELECT [select tc.id as
 id, tc.terminalServerPort.id as terminalServerPortId,
 tc.terminalServerPort.terminalServer.name as terminalServerName,
 tc.terminalServerPort.terminalServer.ipConfig.ipAddress as
 terminalServerIpAddress, tc.terminalServerPort.portNumber as
 terminalServerPort from
 com.windriver.dsm.labmanagement.data.TargetConsole tc where
 tc.target.id = :targetId order by id asc]

有什么想法吗? 谢谢!

【问题讨论】:

  • 你能不能给我们看看hql-查询?
  • 什么意思?这是 HQL 查询。
  • 你的java源代码中的查询?!
  • @Jens 表示您在源代码中编写的查询。这是 Hibernate 生成的查询。
  • ...不,不是,这是 Hibernate 不喜欢的 HQL,它将是代码中的查询:/ 但我同意,最好在上下文中查看它代码而不是日志中。我看不出上面贴的有什么问题。

标签: java hibernate hql


【解决方案1】:

Even when I narrow the HQL to the minimum I still get that exception - , expected in SELECT [select tc.id as id from com.windriver.dsm.labmanagement.data.TargetConsole as tc]

你不应该这样写吗?

[select tc.id as id from com.windriver.dsm.labmanagement.data.TargetConsole tc]

为什么在给表格别名的时候写as

【讨论】:

  • 从技术上讲它更正确,但“as”方式也可以。至少在 JPQL 中确实如此,我承认我不能 100% 确定它是否也适用于 HQL。
  • 根据hibernate documentation,它可以双向工作
【解决方案2】:

我找到了解决办法。

显然我正在使用的休眠版本(休眠 3)不允许为关联实体分配别名。当我删除这些别名时 - 查询有效。

这是正确的代码:

select tc.id, tc.terminalServerPort.id, 
tc.terminalServerPort.terminalServer.name, 
tc.terminalServerPort.terminalServer.IPConfig.IPAddress, 
tc.terminalServerPort.portNumber 
from TargetConsole tc where tc.target.id = :targetId order by id asc

感谢大家的帮助!

【讨论】:

  • 您删除了 order by 子句(您使用的 id 没有别名)。
  • @LluisMartinez 谢谢,我编辑了答案以包含 order by 子句。
【解决方案3】:

您需要为您正在使用的 sessionFactory 配置您的休眠转换器属性。

hibernate.query.factory_class = org.hibernate.hql.ast.ASTQueryTranslatorFactory

【讨论】:

    猜你喜欢
    • 2020-12-17
    • 2014-07-18
    • 1970-01-01
    • 2022-12-07
    • 1970-01-01
    • 1970-01-01
    • 2019-11-06
    相关资源
    最近更新 更多