Spring Data之@Query中的org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML
1.问题描述
org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [update com.easy.springboot.demo_cache.User a set a.password = :password where id=:id]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.errorIfDML(QueryTranslatorImpl.java:311) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:362) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1489) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
问题分析
从错误信息中,可以发现,@Query无法进行DML操作,该如何做呢?问题解决
添加 @Modifying 注解:
package com.easy.springboot.demo_cache
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param
interface UserDao : JpaRepository<User, Long> {
@Query("update #{#entityName} a set a.password = :password where id=:id")
@Modifying
fun updatePassword(@Param("id") id: Long, @Param("password") password: String): Int
}