spring data jpa @Query注解中delete語句報(bào)錯(cuò)的解決
spring data jpa @Query注解中delete語句報(bào)錯(cuò)
項(xiàng)目中需要?jiǎng)h除掉表中的一些數(shù)據(jù)
@Query("delete from EngineerServices es where es.engineerId = ?1")
int deleteByEgId(String engineerId);
但是提示了錯(cuò)誤
org.hibernate.hql.QueryExecutionRequestException: Not supported for DML operations
通過查閱相關(guān)的資料發(fā)現(xiàn),對于執(zhí)行update和delete語句需要添加@Modifying注解
@Modifying
@Query("delete from EngineerServices es where es.engineerId = ?1")
int deleteByEgId(String engineerId);
不過,添加之后運(yùn)行又出現(xiàn)了另一個(gè)錯(cuò)誤
nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query
發(fā)現(xiàn)缺少Transaction,于是添加@Transactional
@Modifying
@Transactional
@Query("delete from EngineerServices es where es.engineerId = ?1")
int deleteByEgId(String engineerId);
到此,這條delete語句終于可以成功的執(zhí)行了。
代碼示例:
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的時(shí)候是這樣寫的
like '%?%'
但是在@Query的value字符串中, 這樣寫
like %?1%
另外,要注意的是: 對于執(zhí)行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)
}
JPA使用@Query注解實(shí)例
1. 一個(gè)使用@Query注解的簡單例子
@Query(value = "select name,author,price from Book b where b.price>?1 and b.price<?2") List<Book> findByPriceRange(long price1, long price2);
2. Like表達(dá)式
@Query(value = "select name,author,price from Book b where b.name like %:name%")
List<Book> findByNameMatch(@Param("name") String name);
3. 使用Native SQL Query
所謂本地查詢,就是使用原生的sql語句(根據(jù)數(shù)據(jù)庫的不同,在sql的語法或結(jié)構(gòu)方面可能有所區(qū)別)進(jìn)行查詢數(shù)據(jù)庫的操作。
@Query(value = "select * from book b where b.name=?1", nativeQuery = true) List<Book> findByName(String name);
4. 使用@Param注解注入?yún)?shù)
@Query(value = "select name,author,price from Book b where b.name = :name AND b.author=:author AND b.price=:price")
List<Book> findByNamedParam(@Param("name") String name, @Param("author") String author,
@Param("price") long price);
5. SPEL表達(dá)式(使用時(shí)請參考最后的補(bǔ)充說明)
'#{#entityName}'值為'Book'對象對應(yīng)的數(shù)據(jù)表名稱(book)。
public interface BookQueryRepositoryExample extends Repository<Book, Long>{
@Query(value = "select * from #{#entityName} b where b.name=?1", nativeQuery = true)
List<Book> findByName(String name);
}
6. 一個(gè)較完整的例子
public interface BookQueryRepositoryExample extends Repository<Book, Long> {
@Query(value = "select * from Book b where b.name=?1", nativeQuery = true)
List<Book> findByName(String name);// 此方法sql將會(huì)報(bào)錯(cuò)(java.lang.IllegalArgumentException),看出原因了嗎,若沒看出來,請看下一個(gè)例子
@Query(value = "select name,author,price from Book b where b.price>?1 and b.price<?2")
List<Book> findByPriceRange(long price1, long price2);
@Query(value = "select name,author,price from Book b where b.name like %:name%")
List<Book> findByNameMatch(@Param("name") String name);
@Query(value = "select name,author,price from Book b where b.name = :name AND b.author=:author AND b.price=:price")
List<Book> findByNamedParam(@Param("name") String name, @Param("author") String author,
@Param("price") long price);
}
7. S模糊查詢注意問題
模糊查詢時(shí)在原生sql中l(wèi)ike后跟隨的值需要單獨(dú)傳,%不支持單獨(dú)出現(xiàn);在使用length查詢數(shù)據(jù)庫時(shí)需要()單獨(dú)括起。
@Repository
public interface SaleschargeRespolity extends JpaRepository<Salescharge,Integer> {
@Query(value = "select p from Salescharge p where ordertime>:startTime and ordertime<:endTime and staffid like :staffidlike and length(staffid)=(length(:staffid)+2)")
List<Salescharge> finAllSubChargeInfo(@Param("startTime") Date startTime, @Param("endTime") Date endTime,@Param("staffid") String staffid, @Param("staffidlike") String staffidlike);
}
8. 解釋例6中錯(cuò)誤的原因
因?yàn)橹付薾ativeQuery = true,即使用原生的sql語句查詢。使用java對象'Book'作為表名來查自然是不對的。只需將Book替換為表名book。
@Query(value = "select * from book b where b.name=?1", nativeQuery = true) List<Book> findByName(String name);
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
struts2中simple主題下<s:fieldError>標(biāo)簽?zāi)J(rèn)樣式的移除方法
這篇文章主要給大家介紹了關(guān)于struts2中simple主題下<s:fieldError>標(biāo)簽?zāi)J(rèn)樣式的移除方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10
spring/springboot整合curator遇到的坑及解決
這篇文章主要介紹了spring/springboot整合curator遇到的坑及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
Java數(shù)據(jù)結(jié)構(gòu)之棧與隊(duì)列實(shí)例詳解
這篇文章主要給大家介紹了關(guān)于Java數(shù)據(jù)結(jié)構(gòu)之棧與隊(duì)列的相關(guān)資料,算是作為用java描述數(shù)據(jù)結(jié)構(gòu)的一個(gè)開始,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-11-11
詳解JDBC的概念及獲取數(shù)據(jù)庫連接的5種方式
Java?DataBase?Connectivity是將Java與SQL結(jié)合且獨(dú)立于特定的數(shù)據(jù)庫系統(tǒng)的應(yīng)用程序編程接口,一種可用于執(zhí)行SQL語句的JavaAPI。本文主要介紹了JDBC的概念及獲取數(shù)據(jù)庫連接的5種方式,需要的可以參考一下2022-09-09
java注解的類型知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理了一篇關(guān)于java注解的類型知識(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-03-03
關(guān)于JAVA8的 Stream學(xué)習(xí)
這篇文章主要介紹了JAVA8 Stream學(xué)習(xí)方法的相關(guān)資料,需要的朋友可以參考下面文章內(nèi)容2021-09-09
Mybatis?Interceptor線程安全引發(fā)的bug問題
這篇文章主要介紹了Mybatis?Interceptor線程安全引發(fā)的bug問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
實(shí)例解析使用Java實(shí)現(xiàn)基本的音頻播放器的編寫要點(diǎn)
這篇文章主要介紹了使用Java實(shí)現(xiàn)基本的音頻播放器的代碼要點(diǎn)實(shí)例分享,包括音頻文件的循環(huán)播放等功能實(shí)現(xiàn)的關(guān)鍵點(diǎn),需要的朋友可以參考下2016-01-01

