码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-07-23 17:29:26      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:

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

@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语句终于可以成功的执行了。

 

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

标签:

原文地址:http://www.cnblogs.com/purplelightning/p/4670941.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!