@Modifying 没有这个注释update和delete会报错

转载的用于自己看

原网址

https://blog.csdn.net/liyanlei5858/article/details/81289858

spring data jpa @Query注解中delete语句报错

https://www.cnblogs.com/purplelightning/p/4670941.html

项目中需要删除掉表中的一些数据

@Query("delete from EngineerServices es where es.engineerId = ?1")

int deleteByEgId(String engineerId);

但是提示了错误

org.hibernate.hql.QueryExecutionRequestException: Not supported for DML operations

通过查阅相关的资料发现,对于执行update和delete语句需要添加@Modifying注解

@Modifying

@Query("delete from EngineerServices es where es.engineerId = ?1")

int deleteByEgId(String engineerId);

不过,添加之后运行又出现了另一个错误

nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query

发现缺少Transaction,于是添加@Transactional


@Modifying

@Transactional

@Query("delete from EngineerServices es where es.engineerId = ?1")

int deleteByEgId(String engineerId);


到此,这条delete语句终于可以成功的执行了。

你可能感兴趣的:(@Modifying 没有这个注释update和delete会报错)