カスタム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);