我正在努力创建一个动态查询,使用弹簧数据在spel中使用null检查日期。我的疑问是:
@Query(nativeQuery = true, value = "select a.* from TABLE a where"
+ "(:#{#DateFrom} is null or a.F_DATE >= trunc(:#{#DateFrom}))")
List<Acc> getAccList(@Param("DateFrom") @Temporal(TemporalType.DATE) Date datefrom);
当我使用有效日期运行此查询时,它可以正常工作,但是当我传递null日期时,我会收到以下错误:
org.springframework.dao.InvalidDataAccessResourceUsageException:无法提取ResultSet; SQL [不适用];嵌套异常是org.hibernate.exception.SQLGrammarException:无法提取ResultSet
堆栈追踪:
Caused by: java.sql.SQLSyntaxErrorException: ORA-00932: inconsistent datatypes: expected NUMBER got BINARY
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:439)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:395)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:802)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:436)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:186)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:521)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:205)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:861)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1145)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1267)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3449)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3493)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1491)
at com.ing.cbp.commons.util.logging.sql.LogablePreparedStatement.executeQuery(LogablePreparedStatement.java:77)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:79)
... 78 common frames omitted
答案 0 :(得分:0)
似乎oracle认为NULL
值是二进制值,而不是在尝试对其执行trunc
时失败。 Casting it to a Date应该有所帮助。因此,生成的查询应如下所示:
select a.* from TABLE a where (:#{#DateFrom} is null
or a.F_DATE >= trunc(CAST(:#{#DateFrom} AS DATE))
答案 1 :(得分:0)
您需要在'where'关键字后添加一个空格。
import AVKit