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
}
    原文作者:一个会写诗的程序员
    原文地址: https://www.jianshu.com/p/8bc9a5e57ee7
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞