【问题标题】:QuerySyntaxException with hql带有 hql 的 QuerySyntaxException
【发布时间】:2016-01-22 08:41:11
【问题描述】:

当我在 tomcat 上部署以下查询时出现异常:

@Query("select max(cast(substring(r.reference,9,4) as decimal(4,0))) from RequestDbo r  where substring(r.reference,0,9) = :referenceRoot")
Long getMaxReference(@Param("referenceRoot") String referenceRoot);


例外是:

org.hibernate.hql.internal.ast.QuerySyntaxException: 期待关闭,在第 1 行第 54 列附近找到 '(' [选择 来自 RequestDbo 的 max(cast(substring(r.reference,9,4) as decimal(4,0))) r where substring(r.reference,0,9) = :referenceRoot]

尽管直接在 sql server 上执行查询是可以的。

我在我的 hql 中没有看到语法错误...

有什么想法吗?

【问题讨论】:

  • 看起来你的整个查询都被 ORM 引用了 []: [select max(cast(substring(r.reference,9,4) as decimal(4,0))) from RequestDbo r where substring(r.reference,0,9) = :referenceRoot]
  • 该类包含其他以相同方式签名且运行良好的查询。示例:@Query("select r from RequestDbo r")

标签: hibernate sql-server-2012 hql


【解决方案1】:

Hibernate 不支持在 JPQL 查询中进行这种类型的转换 as decimal(4,0)。这里有四个解决方法:

  1. 将数据加载到 Java 中,然后获取最高值;
  2. 使用 Java 类来转换所需的值,然后在 JPQL 查询中调用它的构造函数;
  3. 使用原生 SQL;
  4. 创建一个转换所需值的 SQL 函数,并在查询的 MAX 内调用此函数。

我也建议你看看这个问题this question。祝你好运!

【讨论】:

  • 非常感谢博尼法西奥的回答。这个问题是由你提到的原因引起的。我最终将其修复为删除 CAST 并在 java 端而不是在 JPQL 中解析。
猜你喜欢
  • 2018-07-19
  • 1970-01-01
  • 2020-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多