国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring boot jpa 刪除數據和事務管理的問題實例詳解

Spring boot jpa 刪除數據和事務管理的問題實例詳解

2021-01-09 14:57hanghangde Java教程

這篇文章主要介紹了Spring boot jpa 刪除數據和事務管理的問題實例詳解,涉及業務場景的一些知識和遇到的的問題,需要的朋友可以參考。

今天我們介紹的是jpa刪除和事務的一些坑,接下來看看具體內容。

業務場景(這是一個在線考試系統)和代碼:根據問題的id刪除答案

repository層:

?
1
int deleteByQuestionId(Integer questionId);

service 層:

?
1
2
public void deleteChoiceAnswerByQuestionId(Integer questionId) {
choiceAnswerRepository.deleteByQuestionId(questionId);

測試層:

?
1
2
3
4
5
6
7
8
9
10
11
@Test
public void testDeleteByQuestionId() {
 choiceAnswerService.deleteChoiceAnswerByQuestionId(5);
 System.out.println("hehehhe");
 System.out.println("hehehhe");
 System.out.println("hehehhe");
 System.out.println("hehehhe");
 System.out.println("hehehhe");
 System.out.println("hehehhe");
 System.out.println("hehehhe");
}

問題1:如果各層都不加事務管理的話
@Transactional

會報這個錯誤

org.springframework.dao.InvalidDataAccessApiUsageException: No EntityManager with actual transaction available for current thread - cannot reliably process ‘remove' call; nested exception is javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process ‘remove' call

當我們除了query外的modiy和delete外如果沒有各層的方法中進行事務管理的話也就是沒加@Transactional話會報錯

問題2:只在test層加@Transactional
沒有錯誤但是數據并沒有被刪除,在用IDEA的調試是,在執行這個測試方法的過程時還可以在choiceanswer表中進行操作并沒有加鎖事務并沒有起作用

問題3:只在 Repository層加@Transactional

?
1
2
3
4
5
6
7
8
9
10
11
public void deleteChoiceAnswerByQuestionId(Integer questionId) {
choiceAnswerRepository.deleteByQuestionId(questionId);
System.out.println(“hehehhe”);
System.out.println("hehehhe");
// questionRepository.delete(5);
System.out.println(“hehehhe”);
 System.out.println("hehehhe");
 System.out.println("hehehhe");
 System.out.println("hehehhe");
 System.out.println("hehehhe");
}

這時當執行完

?
1
choiceAnswerRepository.deleteByQuestionId(questionId);

 

數據里面被修改

問題4:只在 service層加@Transactional

當只有執行完service內的對應方法時數據才會被刪除

問題5:在service 層和Repository都加上@transactional

當只有執行完service內的對應方法時數據才會被刪除

問題6:只要在test(或者是除了service層和Repository層)加上@Transactional,不管service層和Repository層加不加@Transactional數據都不會被刪除

問題7:

?
1
2
3
4
@Modifying
@Query(“delete from ChoiceAnswer c where c.question.id=?1 “)
@Transactional
int deleteByQuestionId(Integer questionId);

?
1
2
@Transactional
int deleteByQuestionId(Integer questionId);

有什么區別,上面的會直接執行delete語句

下面的會先執行select 再執行delete

總結:

事務管理只有在service加上事務管理才起作用,query不需要事務管理但是delete update但需要事務管理為了不在Service層不加事務管理可以再Repository層的delete uodate加上@transactional 但這樣不能真正保持事務的完整性.

本文關于Spring boot jpa 刪除數據和事務管理的問題實例詳解的介紹就到這里,希望對大家有所幫助,歡迎大家參閱本站其他專題。

原文鏈接:https://www.2cto.com/kf/201611/569207.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 午夜三区 | 美女天堂| 中文字幕久久网 | 亚洲天堂中文字幕在线观看 | 激情欧美一区二区三区 | 欧美精品国产精品 | 欧美午夜一区二区三区免费大片 | 久久精品国产一区二区三区 | 欧美大黄大色一级毛片 | 国产精品日韩精品 | 在线观看a视频 | 亚洲一级毛片 | 午夜视频在线 | 日本久久久久久 | 中文在线观看视频 | 国产日韩欧美三级 | 精产品自偷自拍 | 91精品久久久久久久久 | 狠狠操夜夜爱 | 亚洲一区二区在线免费观看 | 欧美日韩中文字幕在线 | 亚洲国产精品福利 | 狠狠干天天爱 | 亚洲成人免费观看 | 国产一区久久久 | 欧美精品一区二区三区蜜桃视频 | 国产日韩精品一区二区 | 日韩在线免费电影 | 粉嫩欧美一区二区三区高清影视 | 中文久久| 91免费观看视频 | 欧美午夜一区 | 天天久久综合网 | 日本狠狠干| 91污在线观看 | 特黄一级 | 国产一区二区免费视频 | 免费av一区二区三区 | 亚洲精品黄色 | 国产视频一区二区在线 | 精品成人佐山爱一区二区 |