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)文章
關(guān)于Java 中的 Lambda 表達(dá)式
這篇文章主要介紹了關(guān)于Java 中的 Lambda 表達(dá)式,Lambda 表達(dá)式是 Java 涉足函數(shù)式編程的過程。它接受參數(shù)并將其應(yīng)用于表達(dá)式或代碼塊,下面一起進(jìn)入文章查看詳細(xì)內(nèi)容2021-11-11JAVA maven項(xiàng)目使用釘釘SDK獲取token、用戶
這篇文章主要介紹了JAVA maven項(xiàng)目使用釘釘SDK獲取token、用戶,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

解決在Idea 2020.2下使用 Lombok的注解不生效的問題(插件安裝了,依賴也寫了,自動(dòng)注解也設(shè)置了)

SpringBoot基于Sentinel在服務(wù)上實(shí)現(xiàn)接口限流