web-dev-qa-db-ja.com

JpaRepositoryはDML操作ではサポートされていません[クエリの削除]

JPaRepositoryを拡張するインターフェイスで、いくつかのオブジェクトを削除するクエリを作成しましたが、クエリを実行すると例外がスローされます!誰でも私のためにそれを説明でき、事前に感謝できます:)

クエリ:

public interface LimitRepository extends JpaRepository<CLimit, Long> {

  @Query("delete from CLimit l where l.trader.id =:#{#trader.id}")
  void deleteLimitsByTrader(@Param("trader") CTrader trader);

}

私はこのエラーを受け取りました。

例外:

  org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [delete from com.query.domain.CLimit l where l.trader.id =:__$synthetic$__1]
      at org.hibernate.hql.internal.ast.QueryTranslatorImpl.errorIfDML(QueryTranslatorImpl.Java:318)
      at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.Java:369)
      at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.Java:236)
      at org.hibernate.internal.SessionImpl.list(SessionImpl.Java:1300)
      at org.hibernate.internal.QueryImpl.list(QueryImpl.Java:103)
      at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.Java:573)
      at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.Java:495)
      at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:62)
      at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
      at Java.lang.reflect.Method.invoke(Method.Java:498)
      at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.Java:364)
      at com.Sun.proxy.$Proxy98.getSingleResult(Unknown Source)
      at org.springframework.data.jpa.repository.query.JpaQueryExecution$SingleEntityExecution.doExecute(JpaQueryExecution.Java:206)
      at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.Java:78)
      at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.Java:102)
      at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.Java:92)
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.Java:482)
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.Java:460)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.Java:179)
      at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.Java:61)
      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$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.Java:133)
      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:208)
      at com.Sun.proxy.$Proxy88.deleteLimitsByTrader(Unknown Source)
      at com.query.service.impl.LimitServiceImpl.deleteLimitsByTrader(LimitServiceImpl.Java:138)
      at com.query.controller.UserController.deleteUser(UserController.Java:96)
      at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:62)
      at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
      at Java.lang.reflect.Method.invoke(Method.Java:498)
      at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.Java:222)
      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.invokeHandlerMethod(RequestMappingHandlerAdapter.Java:814)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.Java:737)
      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:969)
      at org.springframework.web.servlet.FrameworkServlet.doDelete(FrameworkServlet.Java:893)
      at javax.servlet.http.HttpServlet.service(HttpServlet.Java:656)
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.Java:845)
      at javax.servlet.http.HttpServlet.service(HttpServlet.Java:731)
      at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:303)
      at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:208)
      at org.Apache.Tomcat.websocket.server.WsFilter.doFilter(WsFilter.Java:52)
      at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:241)
      at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:208)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:316)
      at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.Java:126)
      at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.Java:90)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:330)
      at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.Java:114)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:330)
      at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.Java:122)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:330)
      at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.Java:111)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:330)
      at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.Java:169)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:330)
      at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.Java:48)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:330)
      at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilterInternal(BasicAuthenticationFilter.Java:158)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:330)
      at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.Java:205)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:330)
      at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.Java:120)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:330)
      at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.Java:64)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:330)
      at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.Java:53)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:330)
      at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.Java:133)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:330)
      at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.Java:91)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:330)
      at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.Java:213)
      at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.Java:176)
      at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.Java:346)
      at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.Java:262)
      at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:241)
      at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:208)
      at org.Apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.Java:221)
      at org.Apache.catalina.core.StandardContextValve.invoke(StandardContextValve.Java:122)
      at org.Apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.Java:505)
      at org.Apache.catalina.core.StandardHostValve.invoke(StandardHostValve.Java:169)
      at org.Apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.Java:103)
      at org.Apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.Java:956)
      at org.Apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.Java:116)
      at org.Apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.Java:436)
      at org.Apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.Java:1078)
      at org.Apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.Java:625)
      at org.Apache.Tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.Java:318)
      at Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.Java:1142)
      at Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:617)
      at org.Apache.Tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.Java:61)
      at Java.lang.Thread.run(Thread.Java:745)
47
Student

これを試して:

public interface LimitRepository extends JpaRepository<CLimit, Long> {

  @Transactional
  @Modifying
  @Query("delete from CLimit l where l.trader.id =:#{#trader.id}")
  void deleteLimitsByTrader(@Param("trader") CTrader trader);

}

Dbのレコードを変更しようとするときはいつでも、既存のレコードを変更できることをSpringに指示する@Modifyingと同様に@Transactionalにマークする必要があります。
リポジトリメソッドは無効にする必要があります。そうしないと、例外がスローされ続けます。

101
Afridi

私は同じ問題を抱えていたので、@ afridiの答えを試してみました。リポジトリクラスでは@Transactionalアノテーションを使用せず、サービス(および実装)クラスで使用する必要があります。以下の答えを見つけてください。

LimitServiceImpl.Java

import org.springframework.transaction.annotation.Transactional;
...
@Override
@Transactional
public void deleteLimitsByTrader(CTrader trader) {
// here im calling the LimitRepository interface. 
 getEntityRepository().deleteLimitsByTrader(trader);
}

LimitRepository.Java

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
...
public interface LimitRepository extends JpaRepository<CLimit, Long> {

  @Modifying
  @Query("delete from CLimit l where l.trader.id =:#{#trader.id}")
  void deleteLimitsByTrader(@Param("trader") CTrader trader);

}

必ず正しいインポートを使用してください。

24
tk_