カスタムSQLを変更することによって発生した春-JPAの実装上の問題

カスタム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]
    
         
         
    インターフェースリポジトリの修正に直接添加することができるでは@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

おすすめ

転載: www.cnblogs.com/jpfss/p/11162109.html