Hibernate 6.1.x -提取历元函数不再工作

wtlkbnrh  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(202)
@Query(value = "select " +
            "cast(extract(epoch from tx.dateCreated) as long)   " +
            "from TransactionHistory tx " +
            "where tx.id = :id ")
    List<Long> findTimeStats(@Param("id") final String id);

字符串
当我在spring Boot 3.0.6,hibernate 6.1.7.Final和Java 17中尝试上述代码时,它抛出了以下异常:

Caused by: org.hibernate.query.sqm.ParsingException: line 1:26 no viable alternative at input 'selectcast(extract(epochfrom'


完整堆栈跟踪:

Caused by: java.lang.IllegalArgumentException: org.hibernate.query.SemanticException: A query exception occurred [select cast(extract(epoch from tx.dateCreated) as long) from TransactionHistory tx where tx.id = :id ]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:175)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:182)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:760)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:662)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:126)
at jdk.internal.reflect.GeneratedMethodAccessor27.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:360)
at jdk.proxy2/jdk.proxy2.$Proxy156.createQuery(Unknown Source)
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:94)
... 168 more
Caused by: org.hibernate.query.SemanticException: A query exception occurred [select cast(extract(epoch from tx.dateCreated) as long) from TransactionHistory tx where tx.id = :id ]
at org.hibernate.query.hql.internal.StandardHqlTranslator.parseHql(StandardHqlTranslator.java:130)
at org.hibernate.query.hql.internal.StandardHqlTranslator.translate(StandardHqlTranslator.java:77)
at org.hibernate.internal.AbstractSharedSessionContract.lambda$createQuery$2(AbstractSharedSessionContract.java:747)
at org.hibernate.query.internal.QueryInterpretationCacheStandardImpl.createHqlInterpretation(QueryInterpretationCacheStandardImpl.java:141)
at org.hibernate.query.internal.QueryInterpretationCacheStandardImpl.resolveHqlInterpretation(QueryInterpretationCacheStandardImpl.java:128)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:744)
... 176 more
Caused by: org.hibernate.query.sqm.ParsingException: line 1:26 no viable alternative at input 'selectcast(extract(epochfrom'
at org.hibernate.query.hql.internal.StandardHqlTranslator$1.syntaxError(StandardHqlTranslator.java:46)
at org.antlr.v4.runtime.ProxyErrorListener.syntaxError(ProxyErrorListener.java:41)
at org.antlr.v4.runtime.Parser.notifyErrorListeners(Parser.java:543)
at org.antlr.v4.runtime.DefaultErrorStrategy.reportNoViableAlternative(DefaultErrorStrategy.java:310)
at org.antlr.v4.runtime.DefaultErrorStrategy.reportError(DefaultErrorStrategy.java:136)
at org.hibernate.grammars.hql.HqlParser.queryExpression(HqlParser.java:1182)
at org.hibernate.grammars.hql.HqlParser.selectStatement(HqlParser.java:384)
at org.hibernate.grammars.hql.HqlParser.statement(HqlParser.java:315)
at org.hibernate.query.hql.internal.StandardHqlTranslator.parseHql(StandardHqlTranslator.java:116)
... 181 more


同样的代码在Sping Boot 2.7.x、Hibernate 5.x和Java 11中也可以工作。迁移后,此提取部件不再工作。
当我将查询中的提取部分更改为year时,它工作得很好:

cast(extract(year from tx.dateCreated) as long)


看来epoch提取物不起作用。

kmbjn2e3

kmbjn2e31#

将JPQL查询更改为Native查询现在解决了这个问题。

相关问题