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

网友投稿 1157 2022-11-16

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

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

目录spring data jpa @Query注解中delete语句报错项目中需要删除掉表中的一些数据JPA使用@Query注解实例1. 一个使用@Query注解的简单例子2. Like表达式3. 使用Native SQL Query4. 使用@Param注解注入参数5. SPEL表达式(使用时请参考最后的补充说明)6. 一个较完整的例子7. S模糊查询注意问题8. 解释例6中错误的原因

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("uibDHdelete 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 {

@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 http://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注解实例

1. 一个使用@Query注解的简单例子

@Query(value = "select name,author,price from Book b where b.price>?1 and b.price2")

List findByPriceRange(long price1, long price2);

2. Like表达式

@Query(value = "select name,author,price from Book b where b.name like %:name%")

List findByNameMatch(@Param("name") String name);

3. 使用Native SQL Query

所谓本地查询,就是使用原生的sql语句(根据数据库的不同,在sql的语法或结构方面可能有所区别)进行查询数据库的操作。

@Query(value = "select * from book b where b.name=?1", nativeQuery = true)

List findByName(String name);

4. 使用@Param注解注入参数

@Query(value = "select name,author,price from Book b where b.name = :name AND b.author=:author AND b.price=:price")

List findByNamedParam(@Param("name") String name, @Param("author") String author,

@Param("price") long price);

5. SPEL表达式(使用时请参考最后的补充说明)

'#{#entityName}'值为'Book'对象对应的数据表名称(book)。

public interface BookQueryRepositoryExample extends Repository{

@Query(value = "select * from #{#entityName} b where b.name=?1", nativeQuery = true)

List findByName(String name);

}

6. 一个较完整的例子

public interface BookQueryRepositoryExample extends Repository {

@Query(value = "select * from Book b where b.name=?1", nativeQuery = true)

List findByName(String name);// 此方法sql将会报错(java.lang.IllegalArgumentException),看出原因了吗,若没看出来,请看下一个例子

http:// @Query(value = "select name,author,price from Book b where b.price>?1 and b.price2")

List findByPriceRange(long price1, long price2);

@Query(value = "select name,author,price from Book b where b.name like %:name%")

List 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 findByNamedParam(@Param("name") String name, @Param("author") String author,

@Param("price") long price);

}

7. S模糊查询注意问题

模糊查询时在原生sql中like后跟随的值需要单独传,%不支持单独出现;在使用length查询数据库时需要()单独括起。

@Repository

public interface SaleschargeRespolity extends JpaRepository<Salescharge,Integer> {

@Query(value = "select p from Salescharge p where ordertime>:startTime and ordertime<:endtime :staffidlike="" and="" length="" like="" staffid="">

List finAllSubChargeInfo(@Param("startTime") Date startTime, @Param("endTime") Date endTime,@Param("staffid") String staffid, @Param("staffidlike") String staffidlike);

}

8. 解释例6中错误的原因

因为指定了nativeQuery = true,即使用原生的sql语句查询。使用java对象'Book'作为表名来查自然是不对的。只需将Book替换为表名book。

@Query(value = "select * from book b where b.name=?1", nativeQuery = true)

List findByName(String name);

List finAllSubChargeInfo(@Param("startTime") Date startTime, @Param("endTime") Date endTime,@Param("staffid") String staffid, @Param("staffidlike") String staffidlike);

}

8. 解释例6中错误的原因

因为指定了nativeQuery = true,即使用原生的sql语句查询。使用java对象'Book'作为表名来查自然是不对的。只需将Book替换为表名book。

@Query(value = "select * from book b where b.name=?1", nativeQuery = true)

List findByName(String name);

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:创建Maven项目时,GroupId和Artifact Id填写
下一篇:微信小程序开发第九天之视图容器
相关文章

 发表评论

暂时没有评论,来抢沙发吧~