Spring Data之@Query中的org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML

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]
    
  1. 问题分析
    从错误信息中,可以发现,@Query无法进行DML操作,该如何做呢?

  2. 问题解决

添加 @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
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容