iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot-JPA删除不成功,只执行了查询语句问题
  • 798
分享到

SpringBoot-JPA删除不成功,只执行了查询语句问题

2024-04-02 19:04:59 798人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录SpringBoot-JPA删除不成功,只执行了查询语句解决方法JPA中使用delete踩坑记录测试时报错,大概是sql有问题springBoot-JPA删除不成功,只

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);

测试一遍,删除成功。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: SpringBoot-JPA删除不成功,只执行了查询语句问题

本文链接: https://www.lsjlt.com/news/159083.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作