error—JPQL 语句(update)修改数据

本文介绍了解决使用JPQL执行更新操作时遇到的错误。通过添加@Modifying注解,成功解决了“NotsupportedforDMLoperations”错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用JPQL进行更新数据时,update语句提示报错

报错提示如下:

 ERROR 14148 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [update linuy.bean.branch.Branch_Info_Bean B set B.branch_code = :newBranch_code where B.branch_code = :oldBranch_code]; nested exception is java.lang.IllegalStateException: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [update linuy.bean.branch.Branch_Info_Bean B set B.branch_code = :newBranch_code where B.branch_code = :oldBranch_code]] with root cause

org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [update linuy.bean.branch.Branch_Info_Bean B set B.branch_code = :newBranch_code where B.branch_code = :oldBranch_code]

给出的提示是,不支持该数据库操作语句的执行,我检查了jpql确定没有任何错误,但就是报错,我找了好久没发现问题,使用sql语句也不行。原本的jpql如下:

 @Query(value = "update Branch_Info_Bean B set B.branch_code = :newBranch_code where B.branch_code = :oldBranch_code")
 void updateBranch_Code(@Param("newBranch_code")String newBranch_code , @Param("oldBranch_code")String oldBranch_Code);

但是我注意到了一个点,@Query(),本身只是执行查询操作,我在里面使用update那不是会有问题吗?
顺着这一点去找,问题就就很好解决了。只需要在自己的jpql上添加上@Modifying注解即可,告诉ORM这里是执行的写入操作。
添加之后如下,问题顺利解决,此处记录一下。

 @Modifying
 @Query(value = "update Branch_Info_Bean B set B.branch_code = :newBranch_code where B.branch_code = :oldBranch_code")
 void updateBranch_Code(@Param("newBranch_code")String newBranch_code , @Param("oldBranch_code")String oldBranch_Code);
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2025-04-01 14:54:06.566 ERROR 36552 --- [ main] o.s.boot.SpringApplication : Application run failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'datebasechangeListener' defined in file [C:\Users\19242\IdeaProjects\KafkaTest\target\classes\com\ccssoft\LIstener\DatebasechangeListener.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'kafkaProducerService' defined in file [C:\Users\19242\IdeaProjects\KafkaTest\target\classes\com\ccssoft\service\KafkaProducerService.class]: Unsatisfied dependency expressed through constructor parameter 2; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'alarmOutDevRepository' defined in com.ccssoft.Repository.AlarmOutDevRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract void com.ccssoft.Repository.AlarmOutDevRepository.updateIsSendStatus(java.lang.Long,int); Reason: Using named parameters for method public abstract void com.ccssoft.Repository.AlarmOutDevRepository.updateIsSendStatus(java.lang.Long,int) but parameter 'Optional[status]' not found in annotated query 'update Alarmoutdev SET issend = status where id =:id'!; nested exception is java.lang.IllegalStateException: Using named parameters for method public abstract void com.ccssoft.Repository.AlarmOutDevRepository.updateIsSendStatus(java.lang.Long,int) but parameter 'Optional[status]' not found in annotated query 'update Alarmoutdev SET issend = status where id =:id'! 什么问题
04-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

莽晨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值