Spring Data JPA:DML操作不支持

en *_*pes 0 jpql spring-data spring-data-jpa spring-boot amazon-aurora

我已经为Amazon Aurora数据库编写了一个查询,以在我的接口CrudRepositoryExtended中删除一些对象,但是当我执行该查询时,它将引发异常!

@Transactional
@Query("delete from HotelPrice hp where hp.updateDate < ?1 ")
void deletePriceOlderThan   (Date date);
Run Code Online (Sandbox Code Playgroud)

异常跟踪跟踪:

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
Run Code Online (Sandbox Code Playgroud)

Yog*_*ogi 8

为了实现实际上只需要参数绑定的修改查询的执行,方法是使用@Modifying注释查询方法。

@Modifying有效地将所有尚未取消的更改丢弃在EntityManager中。如果您不希望自动清除EntityManager,则可以将@Modifying注释的clearAutomatically属性设置为false

工作代码为:

@Transactional
@Modifying
@Query("delete from HotelPrice hp where hp.updateDate < ?1 ")
void deletePriceOlderThan   (Date date);
Run Code Online (Sandbox Code Playgroud)

进一步阅读:

1. 春季邮局