带有 hql 的 QuerySyntaxException

QuerySyntaxException with hql

我在 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: expecting CLOSE, found '(' near line 1, column 54 [select max(cast(substring(r.reference,9,4) as decimal(4,0))) from RequestDbo r where substring(r.reference,0,9) = :referenceRoot]

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

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

有什么想法吗?

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

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

我也推荐你看看这个问题this question。祝你好运!