spring-jpa通过自定义sql执行修改碰到的问题

博客介绍了JPA自定义SQL编写时的注意点。注解仅用于查询,添加、修改和删除操作需配合其他注解,否则会报错。执行修改操作时,仅添加某注解仍会报错,需在Repository修改接口或调用方法上加事务注解,还给出参考博客链接。

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

在编写自定义SQL的时候需要注意

  • @Query 注解只能用来查询,想要进行添加、修改和删除操作需要配合 @Modifying 注解一同使用
      @Modifying
      @Query("update AdminUser set username=:#{#adminUser.username},password=:#{#adminUser.password} where id=:#{#adminUser.id}")
      int updateInfoById(@Param("adminUser") AdminUser adminUser);
    
    否则执行会报错错误信息如下,提示不支持修改操作
    org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [update com.leimo.module.adminuser.entity.AdminUser set username=:__$synthetic$__1,password=:__$synthetic$__2,updateTime=:__$synthetic$__3 where id=:__$synthetic$__4]; nested exception is java.lang.IllegalStateException: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [update com.leimo.module.adminuser.entity.AdminUser set username=:__$synthetic$__1,password=:__$synthetic$__2,updateTime=:__$synthetic$__3 where id=:__$synthetic$__4]
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:370) ~[spring-orm-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:255) ~[spring-orm-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527) ~[spring-orm-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    
  • 只是添加了 @Modifying 注解在执行修改操作的时候仍然会报错,提示在进行删除和修改的时候需要给方法加上事务
    org.springframework.dao.InvalidDataAccessApiUsageException: Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:402) ~[spring-orm-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:255) ~[spring-orm-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527) ~[spring-orm-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    
    在可以直接在Repository的修改接口上添加 @org.springframework.transaction.annotation.Transactional 注解就可以正常执行修改语句了,或者在调用改接口的方法上添加 @Transactional 事务注解即可
      @Transactional
      @Modifying
      @Query("update AdminUser set username=:#{#adminUser.username},password=:#{#adminUser.password} where id=:#{#adminUser.id}")
      int updateInfoById(@Param("adminUser") AdminUser adminUser);
    

    参考博客
    https://www.jianshu.com/p/9d5bf0e4943f

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值