zoukankan      html  css  js  c++  java
  • spring-jpa通过自定义sql执行修改碰到的问题

    在编写自定义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

    原文地址:https://blog.csdn.net/qq_33430083/article/details/90445618

                                    </div>
  • 相关阅读:
    Fiddler系列教程3:使用Fiddler录制Jmeter性能测试脚本
    PySide6读取EXCLE文档
    C#实现操作DOS命令的方法
    在PyCharm中调用xlrd模块出现 ModuleNotFoundError: No module named 'xlrd' ,但在sublime却可以正常
    pip安装时出现错误:File "D:Python39Scriptspip.exe\__main__.py", line 4, in <module> ModuleNotFoundError: No module named 'pip'
    /ect/fstab与/etc/mtab的区别
    虚拟机中Ubuntu系统修改分辨率
    关于报错Could not load file or assembly的问题
    关于WinCC归档应该知道的事
    vue + element 表单的简单公用组件,表格的集成写法
  • 原文地址:https://www.cnblogs.com/jpfss/p/11162109.html
Copyright © 2011-2022 走看看