JpaRepository 不支持 DML 操作 [删除查询]

Posted

技术标签:

【中文标题】JpaRepository 不支持 DML 操作 [删除查询]【英文标题】:JpaRepository Not supported for DML operations [delete query] 【发布时间】:2017-10-16 18:03:15 【问题描述】:

我已经写了一个查询来删除我扩展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)

【问题讨论】:

我的问题是 delete methodList 返回类型。 :) 我有 2 个问题: 1. 方法返回非无效。 2.我没有添加@Modifying 注意:返回类型可以是Integer/int,而不仅仅是void @standalone 我们应该在 Integer 返回类型中期望什么值。 @SameedTariq 受影响的行数(如在 SQL 删除/更新查询中) 【参考方案1】:

试试这个:

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 中的记录时,您必须将其标记为 @Transactional 以及 @Modifying,这指示 Spring 它可以修改现有记录。

修改查询只能使用void或int/Integer作为返回类型,否则会抛出异常。

【讨论】:

简单点,方法必须作废!我遇到了这个问题,因为在我们的项目中我们期待一个整数,所以我们遇到了同样的问题,即使是注释也是如此。我们花了一点时间才意识到它可能对其他人有所帮助;) @Afridi 我可以返回 int 而不是 void。所以我可以跟踪删除了多少行。 @SudhirOjha 是的,您可以返回 int/Integer(或如前所述的 void)。 这可行,但也请查看@tk 的答案。如果可能的话,@Transactional 注释应该放在别处。 返回类型也可以是Integer/int,不仅是void。值表示受影响的行数。【参考方案2】:

据我所知,我遇到了同样的问题,我尝试了 @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);


确保使用正确的导入。

【讨论】:

我的观察 -@Modifying 应该放在存储库方法而不是服务上。学习。 为什么要使用“事务性”服务?由于存储库,需要事务。如果您将“事务”放在服务中,并且在存储库调用之前/之后需要一些操作,那么即使不需要,您也会(继续)打开事务(然后是资源)。 如果未添加@Transactional,则会抛出以下异常。 javax.persistence.TransactionRequiredException:执行更新/删除查询【参考方案3】:

你忘了在上面的方法中添加两个annotations

@Transactional
@Modifying

【讨论】:

【参考方案4】:

我使用EntityManager.createQuery("your deleteStatement").executeUpdate();解决了这个问题

【讨论】:

【参考方案5】:

添加@Transactional(org.springframework.transaction.annotation.Transactional) 和@Modifying(org.springframework.data.jpa.repository.Modifying) 为我解决了类似的问题

【讨论】:

以上是关于JpaRepository 不支持 DML 操作 [删除查询]的主要内容,如果未能解决你的问题,请参考以下文章

6.1课堂笔记—DML(数据操作语言),DQL查询语句

008-Hadoop Hive sql语法详解3-DML 操作:元数据存储

BigQuery - 分区表上的 DML 支持

客快物流大数据项目(四十五):Spark操作Kudu DML操作

SpringDataJpa——JpaRepository查询功能(转)

JPA:JpaRepository中的实体和想要查询的实体不对应怎么办?