<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                在本系統中,學生的學號除了不能為null以外,其必然也是唯一的。在spring data jpa中如下表示: entity/Student.java ``` @Column(nullable = false, unique = true?) private String sno; ``` * ? 該屬性的默認值為false,設置為true時表示該字段的值具有唯一性 ## 測試 entity/StudentTest.java ``` @Test public void snoUniqueTest() { this.studentRepository.save(this.student); ① this.before(); ② this.studentRepository.save(this.student); ③ } ``` * ① 正常的保存一個學生 * ② 調用this.before,重新生成一個學號相同的新學生 * ③ 保存這個新學生,由于這個新學生與第一次保存的學生學號相同,那么我們預期會發生一個異常 啟動測試后我們將得到如下異常: ``` org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [UK_cxffpo742xolsmubc0erwwf9m]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement ...此處省略多行冗余信息 Caused by: java.sql.SQLIntegrityConstraintViolationException: Duplicate entry '032282' for key 'UK_cxffpo742xolsmubc0erwwf9m' ``` 同null錯誤一樣,unique唯一性校驗產生了DataIntegrityViolationException,在報錯的詳細信息中顯示:在關鍵字'UK_cxffpo742xolsmubc0erwwf9m'上發現了重復條目032282(我們測試學生的學號)。這個關鍵字 `UK_cxffpo742xolsmubc0erwwf9m`是spring為我們自動生成的,我們可以啟動后臺應用: ![](https://img.kancloud.cn/b3/d3/b3d3d3904bb9225bbd56c87e989fd71c_705x72.png) 然后使用navicat來查看生成的student表: ![](https://img.kancloud.cn/ca/a7/caa71ce55ebe6928a02dee20c36b8c1b_802x224.png) 點擊`索引`選項卡: ![](https://img.kancloud.cn/31/ba/31baf9653f4a9a83c16193536c604e71_1508x218.png) 發現該字符串正是spring為sno字段生成的索引名,同時我們還觀察到spring自動將其索引類型設置為UNIQUE,而此時如果新增的sno與歷史數據重復的話,便會發生數據校驗錯誤。 ### 完成測試 確認了異常的類型后,我們使用try catch的方式獲取該異常,并斷言發生了異常。 ``` @Test public void snoUniqueTest() { this.studentRepository.save(this.student); this.before(); boolean called = false; try { this.studentRepository.save(this.student); } catch (DataIntegrityViolationException e) { called = true; } Assertions.assertThat(called).isTrue(); } ``` 我們在此方法中先后調用了兩次`this.studentRepository.save`,如果使用`@Test(expected = DataIntegrityViolationException.class)`來處理此測試,那么即使測試通過,我們是無法確認該異常是第一次調用引發的還是第二次調用引發的(特別當其它的成員修正我們的代碼或是一個月以后的自己修正自己代碼的時候),所以在此我們使用`try catch`的方法來斷言異常。 # 參考文檔 | 名稱 | 鏈接 | 預計學習時長(分) | | --- | --- | --- | | 源碼地址 | [https://github.com/mengyunzhi/spring-boot-and-angular-guild/releases/tag/step4.5.7](https://github.com/mengyunzhi/spring-boot-and-angular-guild/releases/tag/step4.5.7) | - | | @Column | [https://docs.oracle.com/javaee/7/api/javax/persistence/Column.html](https://docs.oracle.com/javaee/7/api/javax/persistence/Column.html) | 5 |
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看