Spring boot jpa 刪除數(shù)據(jù)和事務(wù)管理的問題實(shí)例詳解
今天我們介紹的是jpa刪除和事務(wù)的一些坑,接下來看看具體內(nèi)容。
業(yè)務(wù)場(chǎng)景(這是一個(gè)在線考試系統(tǒng))和代碼:根據(jù)問題的id刪除答案
repository層:
int deleteByQuestionId(Integer questionId);
service 層:
public void deleteChoiceAnswerByQuestionId(Integer questionId) {
choiceAnswerRepository.deleteByQuestionId(questionId);
測(cè)試層:
@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:如果各層都不加事務(wù)管理的話
@Transactional
會(huì)報(bào)這個(gè)錯(cuò)誤
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
當(dāng)我們除了query外的modiy和delete外如果沒有各層的方法中進(jìn)行事務(wù)管理的話也就是沒加@Transactional話會(huì)報(bào)錯(cuò)
問題2:只在test層加@Transactional
沒有錯(cuò)誤但是數(shù)據(jù)并沒有被刪除,在用IDEA的調(diào)試是,在執(zhí)行這個(gè)測(cè)試方法的過程時(shí)還可以在choiceanswer表中進(jìn)行操作并沒有加鎖事務(wù)并沒有起作用
問題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");
}
這時(shí)當(dāng)執(zhí)行完
choiceAnswerRepository.deleteByQuestionId(questionId);
數(shù)據(jù)里面被修改
問題4:只在 service層加@Transactional
當(dāng)只有執(zhí)行完service內(nèi)的對(duì)應(yīng)方法時(shí)數(shù)據(jù)才會(huì)被刪除
問題5:在service 層和Repository都加上@transactional
當(dāng)只有執(zhí)行完service內(nèi)的對(duì)應(yīng)方法時(shí)數(shù)據(jù)才會(huì)被刪除
問題6:只要在test(或者是除了service層和Repository層)加上@Transactional,不管service層和Repository層加不加@Transactional數(shù)據(jù)都不會(huì)被刪除
問題7:
@Modifying @Query(“delete from ChoiceAnswer c where c.question.id=?1 “) @Transactional int deleteByQuestionId(Integer questionId);
與
@Transactional int deleteByQuestionId(Integer questionId);
有什么區(qū)別,上面的會(huì)直接執(zhí)行delete語句
下面的會(huì)先執(zhí)行select 再執(zhí)行delete
總結(jié):
事務(wù)管理只有在service加上事務(wù)管理才起作用,query不需要事務(wù)管理但是delete update但需要事務(wù)管理為了不在Service層不加事務(wù)管理可以再Repository層的delete uodate加上@transactional 但這樣不能真正保持事務(wù)的完整性.
本文關(guān)于Spring boot jpa 刪除數(shù)據(jù)和事務(wù)管理的問題實(shí)例詳解的介紹就到這里,希望對(duì)大家有所幫助,歡迎大家參閱本站其他專題。
相關(guān)文章
Java中實(shí)現(xiàn)文件上傳下載的三種解決方案(推薦)
這篇文章主要介紹了Java中實(shí)現(xiàn)文件上傳下載的三種解決方案的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
Java簡(jiǎn)易計(jì)算器程序設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了Java簡(jiǎn)易計(jì)算器程序設(shè)計(jì)的相關(guān)參考資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-10-10
Mybatis有查詢結(jié)果但存不進(jìn)實(shí)體類的解決方案
這篇文章主要介紹了Mybatis有查詢結(jié)果但存不進(jìn)實(shí)體類的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Java中數(shù)學(xué)相關(guān)類的使用教程
Java是一種廣泛使用的編程語言,它提供了許多數(shù)學(xué)運(yùn)算的函數(shù)和方法,使得開發(fā)者可以輕松地進(jìn)行各種數(shù)學(xué)計(jì)算,下面這篇文章主要給大家介紹了關(guān)于Java中數(shù)學(xué)相關(guān)類使用的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
IDEA生成patch和使用patch的方法實(shí)現(xiàn)
比如你本地修復(fù)的 bug,需要把增量文件發(fā)給客戶,很多場(chǎng)景下大家都需要手工整理修改的文件,并整理好目錄,這個(gè)很麻煩,那有沒有簡(jiǎn)單的技巧呢?本文主要介紹了IDEA生成patch和使用patch的方法實(shí)現(xiàn),感興趣的可以了解一下2023-08-08

