SpringBoot-JPA删除不成功,只执行了查询语句问题
作者:青梅竹马丨两小无猜 时间:2022-09-10 04:13:55
SpringBoot-JPA删除不成功,只执行了查询语句
今天使用JPA自定义了一个删除方法deleteByUserIdAndCommentId发现并没有删除掉对应的数据,只执行了查询语句
Hibernate: select good0_.id as id1_6_, good0_.commentId as commenti2_6_, good0_.userId as userid3_6_ from tbl_good good0_ where good0_.userId=? and good0_.commentId=?
解决方法
在删除方法前加注解@Transactional即可
再次执行时会正常执行
Hibernate: select good0_.id as id1_6_, good0_.commentId as commenti2_6_, good0_.userId as userid3_6_ from tbl_good good0_ where good0_.userId=? and good0_.commentId=?
Hibernate: delete from tbl_good where id=?
Hibernate: select comment0_.id as id1_3_0_, comment0_.articleId as articlei2_3_0_, comment0_.ccomment as ccomment3_3_0_, comment0_.goodNum as goodnum4_3_0_, comment0_.userId as userid5_3_0_, comment0_.userType as usertype6_3_0_ from tbl_comment comment0_ where comment0_.id=?
JPA中使用delete踩坑记录
今天写新模块的一个删除功能,测试的时候出了几个问题。
原代码:
@Query("delete from PrivateMessageEntity ae " +
"where ((ae.sendId=?1 and ae.receiveId=?2)or(ae.sendId=?2 and ae.receiveId=?1)) " +
"and ae.deleted=?3")
int deleteDetailPrivateLetterList(Long sendId, Long receiveId, Boolean deleted);
测试时报错,大概是sql有问题
于是乎我把该hql语句转成sql语句去navicat上执行了一遍,报错显示sql语法有误。
根据指示的错误起始位置,怀疑是别名问题,查了一下,sql的delete在使用别名时 在 delete 与from 之间的表别名不可省略
正确sql如下:
delete a.* from tb_table a
where a.id=1;
修改之后的代码如下:
@Query(value = "delete ae.* from tb_zone_private_message ae " +
"where ((ae.sendId=?1 and ae.receiveId=?2)or(ae.sendId=?2 and ae.receiveId=?1)) " +
"and ae.deleted=?3",nativeQuery = true)
int deleteDetailPrivateLetterList(Long sendId, Long receiveId, Boolean deleted);
这下看起来没问题了,测试了一遍又报错了。
复制报错信息又去查了一下,发现对于执行update和delete语句需要添加@Modifying注解。
修改后代码如下:
@Modifying
@Query(value = "delete ae.* from tb_zone_private_message ae " +
"where ((ae.sendId=?1 and ae.receiveId=?2)or(ae.sendId=?2 and ae.receiveId=?1)) " +
"and ae.deleted=?3",nativeQuery = true)
@Transactional
int deleteDetailPrivateLetterList(Long sendId, Long receiveId, Boolean deleted);
测试一遍,删除成功。
来源:https://blog.csdn.net/qq_41289165/article/details/106206167
标签:SpringBoot,JPA,删除,查询语句
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java内存区域与内存溢出异常详解
2022-09-10 17:01:19
![](https://img.aspxhome.com/file/2023/5/69345_0s.png)
JPA like 模糊查询 语法格式解析
2022-06-16 20:43:42
java 画pdf用itext调整表格宽度、自定义各个列宽的方法
2021-07-12 04:16:10
![](https://img.aspxhome.com/file/2023/6/75766_0s.jpg)
java 中String.equals和==的比较
2023-03-09 08:45:16
Android隐藏标题栏及解决启动闪过标题的实例详解
2023-10-10 12:18:12
Android 之Preference控件基本使用示例详解
2022-10-20 23:42:34
![](https://img.aspxhome.com/file/2023/9/86019_0s.jpg)
Java高级面试题小结
2023-11-23 07:34:00
Java ArrayList实现删除指定位置的元素
2023-11-25 05:34:13
![](https://img.aspxhome.com/file/2023/0/60160_0s.png)
C#中将xml文件反序列化为实例时采用基类还是派生类的知识点讨论
2022-08-04 19:27:32
C#实现简单点餐系统
2021-06-27 22:00:56
![](https://img.aspxhome.com/file/2023/2/80122_0s.jpg)
jdk-logging log4j logback日志系统实现机制原理介绍
2022-03-22 11:45:28
![](https://img.aspxhome.com/file/2023/7/82037_0s.png)
举例详解用Java实现web分页功能的方法
2021-07-27 10:50:19
Java时间复杂度、空间复杂度的深入详解
2023-10-22 01:22:40
![](https://img.aspxhome.com/file/2023/5/69915_0s.png)
java抛出异常的几种情况小结
2022-01-11 05:46:40
![](https://img.aspxhome.com/file/2023/4/68214_0s.jpg)
软件开发基础之设计模式概述
2023-05-14 04:53:07
![](https://img.aspxhome.com/file/2023/4/67434_0s.png)
java并发编程_线程池的使用方法(详解)
2023-03-29 22:23:00
![](https://img.aspxhome.com/file/2023/1/69711_0s.png)
简单了解Java编程中抛出异常的方法
2023-08-07 17:31:49
Java使用JDK与Cglib动态代理技术统一管理日志记录
2021-11-09 00:52:31
详解Spring-bean的循环依赖以及解决方式
2023-08-18 18:30:38
![](https://img.aspxhome.com/file/2023/8/77408_0s.jpg)
Hibernate实现悲观锁和乐观锁代码介绍
2022-04-25 09:43:45