2011-08-23 125 views
2

我使用的遊戲框架,並具有以下代碼:爲什麼我的ConstraintViolationException不被捕獲?

import org.hibernate.exception.ConstraintViolationException; 

... 
public class AuthorService extends Controller 

    ... 

    public static void delete(Long id) 
    { 
     Author author = Author.findById(id);  
     if(author == null) { 
      renderError("Attempt to delete author has failed! The entry could not be found.", 500); 
     } 
     try { 
      author.delete(); 
     } catch (ConstraintViolationException e) { 
      renderError("A foreign key violation exception was thrown trying to delete <b>" + author.name + "</b>!", 500); 
     } 
     renderGSON(jobDescription); 
    } 

    ... 
} 

我不使用級聯刪除的記錄,因爲國外的項目必須設置稍後由用戶選擇的項。

我的問題是,ContraintViolationException從未被捕獲,但我的IDE報告它被拋出?爲什麼是這樣?我想抓住它,處理它並繼續正常的應用程序。

+0

renderError做什麼?是否有可能author.name爲空,生成一個NPE? – PaulJWilliams

回答

2

因爲在Play中,根據code(刪除調用_delete),該方法僅拋出javax.persistence.PersistenceException和RuntimeException。

+1

就是這樣!現在捕獲PersistenceException。謝謝。 –

0

我並不熟悉遊戲,但當會話刷新時(在交易結束時,通常是提交),可能會發生約束違規異常。

相關問題