spring data jpa @Query注解中delete语句报错 : @Modifying注解的使用

简介: spring data jpa @Query注解中delete语句报错项目中需要删除掉表中的一些数据@Query("delete from EngineerServices es where es.

spring data jpa @Query注解中delete语句报错

项目中需要删除掉表中的一些数据

@Query("delete from EngineerServices es where es.engineerId = ?1")
int deleteByEgId(String engineerId);
但是提示了错误

org.hibernate.hql.QueryExecutionRequestException: Not supported for DML operations
通过查阅相关的资料发现,对于执行update和delete语句需要添加@Modifying注解

@Modifying
@Query("delete from EngineerServices es where es.engineerId = ?1")
int deleteByEgId(String engineerId);
不过,添加之后运行又出现了另一个错误

nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query
发现缺少Transaction,于是添加@Transactional

@Modifying
@Transactional
@Query("delete from EngineerServices es where es.engineerId = ?1")
int deleteByEgId(String engineerId);

到此,这条delete语句终于可以成功的执行了。

代码示例:

package com.easy.kotlin.chapter11_kotlin_springboot.dao

import com.easy.kotlin.chapter11_kotlin_springboot.entity.Image
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.PagingAndSortingRepository
import org.springframework.data.repository.query.Param
import org.springframework.transaction.annotation.Transactional

/** Created by jack on 2017/7/17.

@Query注解里面的value和nativeQuery=true,意思是使用原生的sql查询语句.
sql模糊查询like语法,我们在写sql的时候是这样写的

like '%?%'

但是在@Query的value字符串中, 这样写

like %?1%

另外,要注意的是: 对于执行update和delete语句需要添加@Modifying注解
 */

interface ImageRepository : PagingAndSortingRepository<Image, Long> {
    @Query("SELECT a from #{#entityName} a where a.isDeleted=0 and a.category like %?1%")
    fun findByCategory(category: String): MutableList<Image>

    @Query("select count(*) from #{#entityName} a where a.isDeleted=0 and a.url = ?1")
    fun countByUrl(url: String): Int

    @Query("SELECT a from #{#entityName} a where a.isDeleted=0 and a.category like %:searchText%")
    fun search(@Param("searchText") searchText: String, pageable: Pageable): Page<Image>

    @Query("SELECT a from #{#entityName} a where a.isDeleted=0 and a.isFavorite=1")
    fun findAllFavorite(pageable: Pageable): Page<Image>

    @Query("SELECT a from #{#entityName} a where a.isDeleted=0 and a.isFavorite=1 and a.category like %:searchText%")
    fun searchFavorite(@Param("searchText") searchText: String, pageable: Pageable): Page<Image>

    @Modifying
    @Transactional
    @Query("update #{#entityName} a set a.isFavorite=1 where a.id=?1")
    fun addFavorite(id: Long)

    @Modifying
    @Transactional
    @Query("delete from #{#entityName} a where a.id=?1")
    fun delete(id: Long)

}

相关文章
|
28天前
|
XML Java 数据库连接
spring boot 参数的过滤注解与实战
在Spring Boot应用中,对于入参的过滤,通常会涉及到对Web层的数据验证和处理。Spring Boot借助Spring框架提供了强大的验证框架支持,主要基于JSR-303/JSR-380(Bean Validation API)规范,以及Spring自身的@Valid或@Validated注解来实现请求参数的验证。以下是一些常见的使用案例来展示如何对参数进行过滤和验证。
24 1
|
1月前
|
Java 数据库 Spring
【spring(四)】Spring事务管理和@Transactional注解
【spring(四)】Spring事务管理和@Transactional注解
|
7天前
|
XML Java 数据格式
进阶注解探秘:深入Spring高级注解的精髓与实际运用
进阶注解探秘:深入Spring高级注解的精髓与实际运用
23 2
|
7天前
|
XML Java 数据格式
从入门到精通:Spring基础注解的全面解析
从入门到精通:Spring基础注解的全面解析
22 2
从入门到精通:Spring基础注解的全面解析
|
11天前
|
Java 容器
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
12 0
|
30天前
|
Java Apache vr&ar
springmvc报错 nested exception is org.mybatis.spring.MyBatisSystemException:
springmvc报错 nested exception is org.mybatis.spring.MyBatisSystemException:
15 0
|
30天前
|
Java Windows Perl
mybatis+spring报错PropertyAccessException 1: org.springframework.beans.MethodInvocationException
mybatis+spring报错PropertyAccessException 1: org.springframework.beans.MethodInvocationException
8 0
|
30天前
|
前端开发 Java Spring
ssm中spring mvc找不到控制器,报错404
ssm中spring mvc找不到控制器,报错404
14 0
|
1月前
|
Java 数据库 Spring
如何使用Spring Data JPA完成审计功能
如何使用Spring Data JPA完成审计功能
|
6月前
|
缓存 Java Go
解决Spring Data JPA查询存在缓存问题及解决方案
解决Spring Data JPA查询存在缓存问题及解决方案
329 0