@Repository count query - 方法executeUpdate不能用于查询
@Repository count query - Method executeUpdate cannot be used for query
我需要获取今天插入的总行数。
我将 IBM DB2 与 springboot 2 一起用于数据库,并使用 @Repository 执行 CRUD 操作。
然而,我卡在了计数上。
@Transactional
@Modifying
@Query(value = "SELECT COUNT(1) FROM temp.MY_CALL WHERE DATE(INSERT_TS) = CURRENT_DATE ", nativeQuery = true)
int countByCurrentDa();
并得到以下异常
org.hibernate.engine.jdbc.spi.SqlExceptionHelper[0;39m: [jcc][10104][10942][4.9.78] Method executeUpdate cannot be used for query. ERRORCODE=-4476, SQLSTATE=null
org.springframework.orm.jpa.JpaSystemException: could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:353)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:255)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:528)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:149)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
删除 @Modifying
注释 - 您不会修改此查询的任何内容。
根据文档:
Indicates a query method should be considered as modifying query as that changes the way it needs to be executed.
Queries that require a @Modifying
annotation include INSERT, UPDATE, DELETE, and DDL statements.
我需要获取今天插入的总行数。 我将 IBM DB2 与 springboot 2 一起用于数据库,并使用 @Repository 执行 CRUD 操作。
然而,我卡在了计数上。
@Transactional
@Modifying
@Query(value = "SELECT COUNT(1) FROM temp.MY_CALL WHERE DATE(INSERT_TS) = CURRENT_DATE ", nativeQuery = true)
int countByCurrentDa();
并得到以下异常
org.hibernate.engine.jdbc.spi.SqlExceptionHelper[0;39m: [jcc][10104][10942][4.9.78] Method executeUpdate cannot be used for query. ERRORCODE=-4476, SQLSTATE=null
org.springframework.orm.jpa.JpaSystemException: could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:353)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:255)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:528)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:149)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
删除 @Modifying
注释 - 您不会修改此查询的任何内容。
根据文档:
Indicates a query method should be considered as modifying query as that changes the way it needs to be executed.
Queries that require a
@Modifying
annotation include INSERT, UPDATE, DELETE, and DDL statements.