中文字幕第五页-中文字幕第页-中文字幕韩国-中文字幕最新-国产尤物二区三区在线观看-国产尤物福利视频一区二区

Springbootjpa刪除數(shù)據(jù)和事務管理的問題實例詳解

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

成都創(chuàng)新互聯(lián)專注于龍亭企業(yè)網(wǎng)站建設,成都響應式網(wǎng)站建設,商城系統(tǒng)網(wǎng)站開發(fā)。龍亭網(wǎng)站建設公司,為龍亭等地區(qū)提供建站服務。全流程按需網(wǎng)站開發(fā),專業(yè)設計,全程項目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務

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

repository層:

int deleteByQuestionId(Integer questionId);

service 層:

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

測試層:

@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
沒有錯誤但是數(shù)據(jù)并沒有被刪除,在用IDEA的調(diào)試是,在執(zhí)行這個測試方法的過程時還可以在choiceanswer表中進行操作并沒有加鎖事務并沒有起作用

問題3:只在 Repository層加@Transactional

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

這時當執(zhí)行完

choiceAnswerRepository.deleteByQuestionId(questionId);

數(shù)據(jù)里面被修改

問題4:只在 service層加@Transactional

當只有執(zhí)行完service內(nèi)的對應方法時數(shù)據(jù)才會被刪除

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

當只有執(zhí)行完service內(nèi)的對應方法時數(shù)據(jù)才會被刪除

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

問題7:

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

@Transactional
int deleteByQuestionId(Integer questionId);

有什么區(qū)別,上面的會直接執(zhí)行delete語句

下面的會先執(zhí)行select 再執(zhí)行delete

總結(jié):

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

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

分享名稱:Springbootjpa刪除數(shù)據(jù)和事務管理的問題實例詳解
本文網(wǎng)址:http://www.2m8n56k.cn/article44/jgecee.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供云服務器關(guān)鍵詞優(yōu)化企業(yè)建站網(wǎng)站設計公司定制網(wǎng)站網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設
主站蜘蛛池模板: 久久93精品国产91久久综合 | 午夜精品久久久久久91 | 国产精品视频久久久久久 | 欧美一区二区视频在线观看 | 国产一区二区在线观看视频 | 国产黄a三级三级三级 | 日本韩国台湾香港三级 | 精品一区二区三区五区六区 | 美女一级毛片 | 999热成人精品国产免 | 欧美日韩亚洲综合另类ac | 欧美一级毛片日本 | 99re免费99re在线视频手机版 | 男人和女人在床做黄的网站 | 在线精品日韩一区二区三区 | 免费99视频有精品视频高清 | 亚洲第一男人天堂 | 亚洲国产成人精品区 | 男女扒开双腿猛进入爽爽视频 | 国产三级做爰在线观看视频 | 国产精品不卡 | 国产免费福利体检区久久 | 可以看的毛片网站 | 亚洲一二区| 日本加勒比在线视频 | 狠狠88综合久久久久综合网 | 自拍 欧美 | 国产日韩欧美一区二区三区综合 | 国内精品视频九九九九 | 欧美日韩高清在线观看一区二区 | 一区二区三区在线视频观看 | 成人性色生活片全黄 | 姐姐真漂亮在线视频中文版 | 99r精品视频 | 久久99精品久久久久久秒播 | 日韩欧美色综合 | 在线成人亚洲 | 国产精品手机在线播放 | 亚洲成人播放 | 亚洲成a人片毛片在线 | 俄罗斯一级黄色片 |