【发布时间】:2018-07-23 13:23:09
【问题描述】:
我在扩展CrudRepository 的界面中为 Amazon Aurora DB 编写了一个查询,以删除一些对象,但是当我执行查询时它会引发异常!
@Transactional
@Query("delete from HotelPrice hp where hp.updateDate < ?1 ")
void deletePriceOlderThan (Date date);
异常跟踪:
Caused by: java.lang.IllegalStateException: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [delete from com.gamesa.backend.persistence.domain.backend.HotelPrice hp where hp.updateDate < ?1 ]
at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:554)
at org.springframework.data.jpa.repository.query.JpaQueryExecution$SingleEntityExecution.doExecute(JpaQueryExecution.java:208)
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:87)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:116)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:106)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:499)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:477)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:56)
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:282)
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)
... 35 common frames omitted
【问题讨论】:
-
能否为异常添加堆栈跟踪
-
你能在你的删除方法上添加'@Modifying'吗,这有效地删除了所有在EntityManager中仍然挂起的未刷新的更改。如果您不希望 EntityManager 被自动清除,您可以将 @Modifying 注释的 clearAutomatically 属性设置为 false
-
@Yogi,它有效,谢谢。请转换为答案
-
@enLopes - 将我的评论添加为答案
标签: spring-boot spring-data spring-data-jpa jpql amazon-aurora