@query("delete from engineerrvices 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 engineerrvices 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 engineerrvices 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.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注解里面的value和nativequery=true,意思是使用原生的sql查询语句.sql模糊查询like语法,我们在写sql的时候是这样写的like '%?%'但是在@query的value字符串中, 这样写like %?1%另外,要注意的是: 对于执行update和delete语句需要添加@modifying注解 */ interface imagerepository : pagingandsortingrepository<image, long> { @query("lect a from #{#entityname} a where a.isdeleted=0 and a.category like %?1%") fun findbycategory(category: string): mutablelist<image> @query("lect count(*) from #{#entityname} a where a.isdeleted=0 and a.url = ?1") fun countbyurl(url: string): int @query("lect a from #{#entityname} a where a.isdeleted=0 and a.category like %:archtext%") fun arch(@param("archtext") archtext: string, pageable: pageable): page<image> @query("lect a from #{#entityname} a where a.isdeleted=0 and a.isfavorite=1") fun findallfavorite(pageable: pageable): page<image> @query("lect a from #{#entityname} a where a.isdeleted=0 and a.isfavorite=1 and a.category like %:archtext%") fun archfavorite(@param("archtext") archtext: string, pageable: pageable): page<image> @modcad2009教程ifying @transactional @query("update #{#entityname} a t 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) }
@query(value = "lect name,author,price from book b where b.price>?1 and b.price<?2")list<book> findbypricerange(long price1, long price2);
@query(value = "lect name,author,price from book b where b.name like %:name%")list<book> findbynamematch(@param("name") string name);
所谓本地查询,就是使用原生的sql语句(根据数据库的不同,在sql的语法或结构方面可能有所区别)进行查询数据库的操作。
@query(value = "lect * from book b where b.name=?1", nativequery = true)list<book> findbyname(string name);
@query(value = "lect name,author,price from book b wheraddicte 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);
‘#{#entityname}’值为’book’对象对应的数据表名称(book)。
public interf转正申请ace bookqueryrepositoryexample extends r云南旅游学院epository<book, long>{ @query(value = "lect * from #{#entityname} b where b.name=?1", nativequery = true) list<book> findbyname(string name);}
public interface bookqueryrepositoryexample extends repository<book, long> { @query(value = "lect * from book b where b.name=?1", nativequery = true) list<book> findbyname(string name);// 此方法sql将会报错(java.lang.illegalargumentexception),看出原因了吗,若没看出来,请看下一个例子 @query(value = "lect name,author,price from book b where b.price>?1 and b.price<?2") list<book> findbypricerange(long price1, long price2); @query(value = "lect name,author,price from book b where b.name like %:name%") list<book> findbynamematch(@param("name") string name); @query(value = "lect 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); }
模糊查询时在原生sql中like后跟随的值需要单独传,%不支持单独出现;在使用length查询数据库时需要()单独括起。
@repositorypublic interface saleschargerespolity extends jparepository<salescharge,integer> { @query(value = "lect 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); }
因为指定了nativequery = true,即使用原生的sql语句查询。使用java对象’book’作为表名来查自然是不对的。只需将book替换为表名book。
@query(value = "lect * from book b where b.name=?1", nativequery = true)list<book> findbyname(string name);
以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。
本文发布于:2023-04-04 01:34:56,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/3e212948e2de94feae43de469365f8fe.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:spring data jpa @Query注解中delete语句报错的解决.doc
本文 PDF 下载地址:spring data jpa @Query注解中delete语句报错的解决.pdf
留言与评论(共有 0 条评论) |