JPA执行本机SQL服务器查询

JPA执行本机SQL服务器查询

本文介绍了如何在不使用结果集的情况下使用Spring Data JPA执行本机SQL服务器查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Spring Data JPA和Hibernate创建一个存储库,其中,我添加了两个查询来启用或禁用插入标识,以便我可以使用指定的ID添加值。
我认为在界面中创建一个本地查询是正确的。

pre $ public interface PartRepo extends PagingAndSortingRepository< Part ,Long> {

//其他方法

@Query(value =SET IDENTITY_INSERT PART ON,nativeQuery = true)
public void enableInsertIdentity();

@Query(value =SET IDENTITY_INSERT PART OFF,nativeQuery = true)
public void disableInsertIdentity();


但是当hibernate执行其中一个 SET IDENTITY_INSERT 查询我收到此错误:

  com.microsoft.sqlserver.jdbc.SQLServerException:该声明没有返回结果集。 

这是完整的堆栈跟踪:

  Hibernate:SET IDENTITY_INSERT PART ON 
2016-02-08 15:20:06.069 WARN 7580 --- [nio-8081-exec-1] ohengine.jdbc.spi。 SqlExceptionHelper:SQL错误:0,SQLState:null
2016-02-08 15:20:06.069错误7580 --- [nio-8081-exec-1] ohengine.jdbc.spi.SqlExceptionHelper:语句确实不返回结果集。
2016-02-08 15:20:06.527 ERROR 7580 --- [nio-8081-exec-1] oaccC [。[。[/]。[dispatcherServlet]:Servlet.service()for servlet [dispatcherServlet ]在上下文路径[]抛出异常[请求处理失败;嵌套异常是javax.persistence.PersistenceException:org.hibernate.exception.GenericJDBCException:无法提取ResultSet]的根本原因

com.microsoft.sqlserver.jdbc.SQLServerException:该语句未返回结果组。在com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:191)

在com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:450)
在com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement $ PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:383)
在com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:6703)
。在COM .microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1929)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:184)
at com.microsoft.sqlserver .jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:159)
在com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:295)
在org.hibernate.engine.jdbc.internal .ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82)
在org.hibernate.loader.Loader.getResultSet(Loader.java:2066)
在org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1863)
在org.hibernate.loader.Loader .executeQueryStatement(Loader.java:1839)
在org.hibernate.loader.Loader.doQuery(Loader.java:910)
在org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355 )
在org.hibernate.loader.Loader.doList(Loader.java:2554)
在org.hibernate.loader.Loader.doList(Loader.java:2540)
at org。 hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2370)
在org.hibernate.loader.Loader.list(Loader.java:2365)
在org.hibernate.loader.custom.CustomLoader。 list(CustomLoader.java:353)
at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1909)
at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:311)
at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:141)
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)
org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:495)
at org.springframework。 data.jpa.repository.query.JpaQueryExecution $ SingleEntityExecution.doExecute(JpaQueryExecution.java:202)
在org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:74)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:97)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:88 )
在org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:395)
在org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor .invoke(RepositoryFactorySupport.java:373)
at org.springframework.aop.framework.ReflectiveMethodInvo在org.springframework.data.repository.core.support.RepositoryFactorySupport $ DefaultMethodInvokingMethodInterceptor.invoke cation.proceed(ReflectiveMethodInvocation.java:179)
(RepositoryFactorySupport.java:486)
。在org.springframework.aop。 framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
在org.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java:99)
在org.springframework.transaction.interceptor.TransactionAspectSupport .invokeWithinTransaction(TransactionAspectSupport.java:281)
在org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed
(ReflectiveMethodInvocation .java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor $ CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:122)$ b在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
$ b在org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
。在组织.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
在org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
在com.sun.proxy 。$ Proxy126.enableInsertIdentity(Unknown Source)
at it.cdc.snp.business.supply.FeedingBusinessAnag.feedAllParts(FeedingBusinessAnag.java:40)
at it.cdc.snp.business.supply.FeedingBusinessAnag $$ FastClas在org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
处org.springframework.aop.framework.CglibAopProxy $ sbySpringCGLIB $$ beb28ba9.invoke(<生成>)
$ CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.cloud.context.config.StandardBeanLifecycleDecorator $ 2 .invoke(StandardBeanLifecycleDecorator.java:85)
在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
在org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept (CglibAopProxy.java:653)
at it.cdc.snp.business.supply.FeedingBusinessAnag $$ EnhancerBySpringCGLIB $$ aaf07810.feedAllParts(< generated>)
at it.cdc.snp.business。 supply.FeedingBusinessAnag $$ FastClassBySpringCGLIB $$ beb28ba9.invoke(< generated>)
a吨org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
。在org.springframework.aop.framework.CglibAopProxy $ CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
。在有机.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
在org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:133)
在org.springframework.aop .support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:121)
在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
在org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at it.cdc.snp.business.supply.FeedingBusinessAnag $$ EnhancerBySpringCGLIB $$ 5dc38301.feedAllParts(< generated>)
at it.cdc。 snp.services.rest.GenericRestApi.saveAnag(G enericRestApi.java:70)
at it.cdc.snp.services.rest.GenericRestApi $$ FastClassBySpringCGLIB $$ 398e7cc1.invoke(< generated>)
at org.springframework.cglib.proxy.MethodProxy .invoke(MethodProxy.java:204)
在org.springframework.aop.framework.CglibAopProxy $ CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (ReflectiveMethodInvocation.java:157)
在org.springframework.cloud.context.config.StandardBeanLifecycleDecorator $ 2.invoke(StandardBeanLifecycleDecorator.java:85)
在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed( ReflectiveMethodInvocation.java:179)
在org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
在it.cdc.snp.services.rest.GenericRestApi $$ EnhancerBySpringCGLIB $$ a51a0728.saveAnag(< generated>)
at it.cdc.snp.services.rest.GenericRestApi $$ Fas tClassBySpringCGLIB $$ 398e7cc1.invoke(小于生成>)
。在org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
。在org.springframework.aop.framework.CglibAopProxy $ CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed( DelegatingIntroductionInterceptor.java:133)在org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:121

在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:在org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept 179)
(CglibAopProxy.java:653)
在it.cdc.snp.services.rest.GenericRestApi $$ EnhancerBySpringCGLIB $$ b5080d19。 saveAnag(<生成>)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
在sun.reflect.NativeMethodAccessorImpl.invoke(来源不明)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(来源不明)
是java在org.springframework.web.method.support.InvocableHandlerMethod.doInvoke .lang.reflect.Method.invoke(来源不明)
(InvocableHandlerMethod.java:221)
。在org.springframework.web.method。在org.springframework上为
提供org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
。 web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)
在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
在org.springframework.w eb.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
处org.springframework org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
。 web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
处org.springframework.web.servlet org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
。 FrameworkServlet.doGet(FrameworkServlet.java:857)
位于javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
位于org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet。
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
在org.apache.tomcat.websocket.server.WsFil ter.doFilter(WsFilter.java:52)
在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
在org.apache.catalina.core.ApplicationFilterChain.doFilter( ApplicationFilterChain.java:206)
at org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration $ ApplicationContextHeaderFilter.doFilterInternal(EndpointWebMvcAutoConfiguration.java:295)
at org.springframework.web.filter.OncePerRequestFilter.doFilter( OncePerRequestFilter.java:107)在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:239)
在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java: 206)
在org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:102)在org.springframework.web.filter.OncePerRequestFilter.doFilter
(OncePerRequestFilter.java:107)
在org.apache.catalina.co re.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
处org.springframework.web.filter.HiddenHttpMethodFilter org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
。 doFilterInternal(HiddenHttpMethodFilter.java:77)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain。 Java的:239)
在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
在org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
在org.apache.catalina.core.ApplicationFilterChain。的doFilter(ApplicationFilterChain.java:206)
在org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:68)
在org.springframework.web.filter.OncePerRequestFilter.doFilter( OncePerRequestFilter.java:107)在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:239)
在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219 206)

在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)$ b在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
$ b在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
。在组织.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.core.StandardEngineValve.in voke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor。
at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint。 java:1521)
at org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1478)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
在java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知源)
在org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)
在java.lang.Thread.run(Unknown Source)

我不知道该做什么这一点,我试图在谷歌上搜索,但我什么也没找到

解决方案

默认情况下, @Query 被认为是选择查询。要将其视为修改查询,请使用注释:

所以,你应该用下面的方式写你的仓库:

  public interface PartRepo extends PagingAndSortingRepository< Part,Long> {
@Modifying
@Query(value =SET IDENTITY_INSERT PART ON,nativeQuery = true)
public void enableInsertIdentity();

@Modifying
@Query(value =SET IDENTITY_INSERT PART OFF,nativeQuery = true)
public void disableInsertIdentity();
}


I am using Spring Data JPA and Hibernate to create a repository in which, among the other things, I have added two queries to enable or disable insert identity so that i can add values with a specified ID.I thought that it was correct to create a native query in the interface

   public interface PartRepo extends  PagingAndSortingRepository <Part,Long > {

        //other methods

        @Query(value = "SET IDENTITY_INSERT PART ON", nativeQuery = true)
        public void enableInsertIdentity();

        @Query(value = "SET IDENTITY_INSERT PART OFF", nativeQuery = true)
        public void disableInsertIdentity();

    }

but when hibernate executes one of the SET IDENTITY_INSERT queries i receive this error:

com.microsoft.sqlserver.jdbc.SQLServerException: The statement did not return a result set.

This is the full stacktrace:

Hibernate: SET IDENTITY_INSERT PART ON
2016-02-08 15:20:06.069  WARN 7580 --- [nio-8081-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: null
2016-02-08 15:20:06.069 ERROR 7580 --- [nio-8081-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : The statement did not return a result set.
2016-02-08 15:20:06.527 ERROR 7580 --- [nio-8081-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not extract ResultSet] with root cause

com.microsoft.sqlserver.jdbc.SQLServerException: The statement did not return a result set.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:191)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:450)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:383)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:6703)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1929)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:184)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:159)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:295)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:2066)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1863)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1839)
    at org.hibernate.loader.Loader.doQuery(Loader.java:910)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355)
    at org.hibernate.loader.Loader.doList(Loader.java:2554)
    at org.hibernate.loader.Loader.doList(Loader.java:2540)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2370)
    at org.hibernate.loader.Loader.list(Loader.java:2365)
    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:353)
    at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1909)
    at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:311)
    at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:141)
    at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)
    at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:495)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$SingleEntityExecution.doExecute(JpaQueryExecution.java:202)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:74)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:97)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:88)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:395)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:373)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$DefaultMethodInvokingMethodInterceptor.invoke(RepositoryFactorySupport.java:486)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:122)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    at com.sun.proxy.$Proxy126.enableInsertIdentity(Unknown Source)
    at it.cdc.snp.business.supply.FeedingBusinessAnag.feedAllParts(FeedingBusinessAnag.java:40)
    at it.cdc.snp.business.supply.FeedingBusinessAnag$$FastClassBySpringCGLIB$$beb28ba9.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.cloud.context.config.StandardBeanLifecycleDecorator$2.invoke(StandardBeanLifecycleDecorator.java:85)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
    at it.cdc.snp.business.supply.FeedingBusinessAnag$$EnhancerBySpringCGLIB$$aaf07810.feedAllParts(<generated>)
    at it.cdc.snp.business.supply.FeedingBusinessAnag$$FastClassBySpringCGLIB$$beb28ba9.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:133)
    at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:121)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
    at it.cdc.snp.business.supply.FeedingBusinessAnag$$EnhancerBySpringCGLIB$$5dc38301.feedAllParts(<generated>)
    at it.cdc.snp.services.rest.GenericRestApi.saveAnag(GenericRestApi.java:70)
    at it.cdc.snp.services.rest.GenericRestApi$$FastClassBySpringCGLIB$$398e7cc1.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.cloud.context.config.StandardBeanLifecycleDecorator$2.invoke(StandardBeanLifecycleDecorator.java:85)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
    at it.cdc.snp.services.rest.GenericRestApi$$EnhancerBySpringCGLIB$$a51a0728.saveAnag(<generated>)
    at it.cdc.snp.services.rest.GenericRestApi$$FastClassBySpringCGLIB$$398e7cc1.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:133)
    at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:121)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
    at it.cdc.snp.services.rest.GenericRestApi$$EnhancerBySpringCGLIB$$b5080d19.saveAnag(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration$ApplicationContextHeaderFilter.doFilterInternal(EndpointWebMvcAutoConfiguration.java:295)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:102)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:68)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

And I have no idea what to do at this point, i have tried searching on Google but I found nothing

解决方案

By default @Query is considered to be a select query. To make it be treated as a modifying query, use @Modifying annotation:

So, you should write your repository the following way:

public interface PartRepo extends PagingAndSortingRepository<Part,Long> {
    @Modifying
    @Query(value = "SET IDENTITY_INSERT PART ON", nativeQuery = true)
    public void enableInsertIdentity();

    @Modifying
    @Query(value = "SET IDENTITY_INSERT PART OFF", nativeQuery = true)
    public void disableInsertIdentity();
}

这篇关于如何在不使用结果集的情况下使用Spring Data JPA执行本机SQL服务器查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 12:39