具有聚合功能的查询方法的验证失败

时间:2018-11-19 13:44:05

标签: spring hibernate

我在Spring中编写了此查询方法:

@Query("SELECT MAX(TO_NUMBER(ban.codiBanca)) FROM DBanche ban WHERE LENGTH(TRIM(TRANSLATE(ban.codiBanca, '0123456789', ' '))) IS NULL")
public BigDecimal getMaxCodiBanca();

codiBanca的字段类型为VARCHAR(20)。

启动应用程序时出现此错误:

Caused By: java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.math.BigDecimal it.gov.mef.efim.dao.repository.base.BancaDao.getMaxCodiBanca()!
    at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:92)
    at org.springframework.data.jpa.repository.query.SimpleJpaQuery.<init>(SimpleJpaQuery.java:62)
    at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:72)
    at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromQueryAnnotation(JpaQueryFactory.java:53)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:136)
    Truncated. see log file for complete stacktrace
Caused By: java.lang.IllegalStateException: No data type for node: org.hibernate.hql.internal.ast.tree.AggregateNode 
 \-[AGGREGATE] AggregateNode: 'MAX'
    \-[METHOD_CALL] MethodNode: '('
       +-[METHOD_NAME] IdentNode: 'TO_NUMBER' {originalText=TO_NUMBER}
       \-[EXPR_LIST] SqlNode: 'exprList'
          \-[DOT] DotNode: 'dbanche0_.CODI_BANCA' {propertyName=codiBanca,dereferenceType=PRIMITIVE,getPropertyPath=codiBanca,path=ban.codiBanca,tableAlias=dbanche0_,className=it.gov.mef.efim.dao.entities.DBanche,classAlias=ban}
             +-[ALIAS_REF] IdentNode: 'dbanche0_.SEQU_ID_BANCA' {alias=ban, className=it.gov.mef.efim.dao.entities.DBanche, tableAlias=dbanche0_}
             \-[IDENT] IdentNode: 'codiBanca' {originalText=codiBanca} 

如何解决此错误?

1 个答案:

答案 0 :(得分:2)

您似乎正在使用本机查询,即SQL语法特定于数据库供应商。

因此,将属性nativeQuery=true添加到您的@Query中,如下所示。

@Query(nativeQuery=true, value = "SELECT MAX(TO_NUMBER(ban.codiBanca)) FROM DBanche ban WHERE LENGTH(TRIM(TRANSLATE(ban.codiBanca, '0123456789', ' '))) IS NULL")
public BigDecimal getMaxCodiBanca();

希望这会有所帮助!

如果仍然遇到任何问题,请在此处发布。