Udemy Course Spring JPA 4
多建幾筆資料列,在data.sql內
在CourseRepository中建立刪除entity的Method叫deleteById
在DemoApplication中呼叫上面新建的deleteById Method
執行得transaction error:
因為delete這個動作是實際會造成資料改變(即data transaction)
舉例有一筆交易是一連串動作如:
由我的帳戶轉1000元至朋友的帳戶
那麼交易順序將會是
- 刪除我帳戶1000元
- 朋友帳戶新增1000元
假若第一步成功,但是第二步失敗
整個交易應當roll back
所以在上述程式中,我們可將delete這個交易交由Spring管理
在CourseRepository中加上Spring的transaction的annotation
再次執行DemoApplication得
http://localhost:8090/h2-console看結果