Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that only the query statement was executed when SpringBoot-JPA was deleted unsuccessfully?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to solve the problem of SpringBoot-JPA deletion is not successful only the implementation of the query sentence, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian with you to understand.

SpringBoot-JPA deletion was not successful, only the query statement was executed

Use JPA to customize a delete method deleteByUserIdAndCommentId found that the corresponding data is not deleted, only the query statement is executed

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=? Solution method

You can add @ Transactional before deleting the method.

It will be executed normally when it is executed again.

Hibernate: select good0_.id as id1_6_, good0_.commentId as commenti2_6_, good0_.userId as userid3_6_ from tbl_good good0_ where good0_.userId=? Using delete trampling record in 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

Today, I wrote a delete function of the new module, and there were several problems during the test.

Original code:

@ 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); the test times is wrong, maybe there is something wrong with sql

So I converted the hql statement into a sql statement and executed it on navicat, and the error report showed that the sql syntax was incorrect.

According to the error starting position indicated, it is suspected to be an alias problem. Check that the table alias between delete and from in sql's delete can not be omitted when using aliases.

The correct sql is as follows:

Delete a.* from tb_table a where a.id=1

The modified code is as follows:

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

This seems to be no problem. I tested it again and reported it wrong again.

The copy error message is checked again and it is found that the @ Modifying annotation needs to be added to execute update and delete statements.

The modified code is as follows:

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)

It was tested again and deleted successfully.

Thank you for reading this article carefully. I hope the article "how to solve the problem of unsuccessful deletion of SpringBoot-JPA only executed the query sentence" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report