Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the error report of delete statement in spring data jpa @ Query annotation

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to solve the error report of delete sentence in spring data jpa @ Query annotation". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how to solve the error report of delete statement in the spring data jpa @ Query annotation.

In the delete statement error item in the spring data jpa @ Query annotation, you need to delete some data from the table @ Query ("delete from EngineerServices es where es.engineerId =? 1") int deleteByEgId (String engineerId)

But it prompted an error.

Org.hibernate.hql.QueryExecutionRequestException: Not supported for DML operations

By consulting the relevant data, it is found that @ Modifying annotations need to be added to execute update and delete statements.

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

However, another error occurred in the run after the addition

Nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query

Found that Transaction was missing, so add @ Transactional

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

At this point, the delete statement can finally be successfully executed.

Code example:

Value and nativeQuery=true in package com.easy.kotlin.chapter11_kotlin_springboot.dao import com.easy.kotlin.chapter11_kotlin_springboot.entity.Imageimport org.springframework.data.domain.Pageimport org.springframework.data.domain.Pageableimport org.springframework.data.jpa.repository.Modifyingimport org.springframework.data.jpa.repository.Queryimport org.springframework.data.repository.PagingAndSortingRepositoryimport org.springframework.data.repository.query.Paramimport org.springframework.transaction.annotation.Transactional / * * Created by jack on 2017/7/17.@Query annotations It means to use the native sql query statement. SQL fuzzy query like syntax, we write like'%?% 'when we write sql, but in the value string of @ Query, we write like%? 1% in addition Note that @ Modifying annotations need to be added to execute update and delete statements * / interface ImageRepository: PagingAndSortingRepository {@ Query ("SELECT a from # {# entityName} a where a.isDeleted=0 and a.category like%? 1%") fun findByCategory (category: String): MutableList @ 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 @ Query ("SELECT a from # {# entityName} a where a.isDeleted=0 and a.isFavorite=1") fun findAllFavorite (pageable: Pageable): Page @ 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 @ 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)} JPA uses @ Query to annotate instance 1. A simple example of using @ Query annotations @ Query (value = "select name,author,price from Book b where b.price >? 1 and b.price

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report