2012-06-02 61 views
2

我正嘗試使用JPA創建簡單的數據庫連接。 它工作正常,但是當我嘗試拋出一個異常到客戶端,我得到的錯誤:GWT + JPA Persistence.Exception源代碼未找到

[ERROR] [browsereditor] - Line 210: No source code is available for type javax.persistence.EntityExistsException; did you forget to inherit a required module? 

[ERROR] [browsereditor] - Line 212: No source code is available for type javax.persistence.EntityNotFoundException; did you forget to inherit a required module? 

我得到了發展模式沒有錯誤,它編譯罰款,但是當應用模塊加載有我在哪裏得到錯誤。

我在服務器/作曲者和客戶端/演示類所需的進口

import javax.persistence.EntityExistsException; 
import javax.persistence.EntityNotFoundException; 

我還添加了以下jar添加到類路徑和構建路徑:

javax.persistence.jar

jpa-annotations-source.jar(http://code.google.com/p/google-web-toolkit/issues/detail?id=1830#c14)

我也嘗試添加到gwt.xml

<source path='client'/> 
<source path='shared'/> 
<source path='server'/> 

如何告訴Eclipse在哪裏可以找到源代碼的任何想法?

感謝

下面是代碼:

//在服務器Presenter.class

創建一個從Composer.class的作曲家createComposer(上圖)

public static Composer createComposer(String name) 
     throws EntityExistsException { 
    Composer comp = new Composer(); 
    comp.setName(name); 
    comp.setId(1); 

    EntityManager entityManager = entityManager(); 
    entityManager.getTransaction().begin(); 
    entityManager.persist(comp); 
    entityManager.getTransaction().commit(); 
    entityManager.close(); 

    return comp; 
} 

///消防要求

req.fire(new Receiver<ComposerProxy>() { 

         public void onSuccess(ComposerProxy arg0) { 

          ComposerProxy comp; 
          comp = arg0; 
         } 

         public void onFailure(Throwable caught) 
           throws Throwable { 
          // Convenient way to find out which exception 
          // was thrown. 
          try { 
           throw caught; 
          } catch (EntityExistsException e) { 

          } catch (EntityNotFoundException e) { 

          } 
         }}); 
       }}); 


[ERROR] [browsereditor] - Line 210: No source code is available for type javax.persistence.EntityExistsException; did you forget to inherit a required module? 
[ERROR] [browsereditor] - Line 212: No source code is available for type javax.persistence.EntityNotFoundException; did you forget to inherit a required module? 

回答

0

您不能使用類型,如EntityExistsExceptionEntityNotFoundException在客戶端GWT代碼中。

這些是普通的Java類,GWT不知道如何將它們翻譯成JavaScript。

您的客戶端代碼中只能使用非常有限的外部庫。這些庫(例如Visualisation)專門爲客戶端設計和準備,並要求在應用程序的模塊中繼承它們的GWT模塊。

我覺得你真的想要做的是這樣的事情:

public void onFailure(ServerFailure failure) throws Throwable { 
    if(failure.getExceptionType().equals("javax.persistence.EntityExistsException")){ 
      ... 
    }else if(failure.getExceptionType().equals("javax.persistence.EntityNotFoundException")){ 
     ... 
    } 
} 

因爲你可以讀取服務器端異常的字符串類型,見Javadoc for ReceiverServerFailure

0

感謝Piotr的幫助。

這裏是什麼,我終於做到了代碼:在客戶端

代碼

req.fire(new Receiver<ComposerProxy>() { 

         public void onSuccess(ComposerProxy arg0) { 

          ComposerProxy comp; 
          comp = arg0; 
         } 

         public void onFailure(ServerFailure failure) { 

          serverError.getServerError(failure, 
            "onAddButtonClicked"); 

         } 

        }); 

我創建了一個類來處理在服務器中的錯誤

public class ServerError { 

public ServerError() { 
} 

public void getServerError(ServerFailure failure, String message) { 
    // Duplicate Key Error 
    if (failure.getMessage().contains(
      "IntegrityConstraintViolationException")) { 

     Window.alert("Duplicate Key " + message); 
     return; 
    } 
    // Connection Error 
    if (failure.getMessage().contains("NonTransientConnectionException")) { 
     Window.alert("Connection error "); 
     return; 
    } 
    // TimeOut Error 
    if (failure.getMessage().contains("TimeoutException")) { 
     Window.alert("Timeout Error" + message); 
     return; 
    } 
    // Other Error 
    else { 
     Window.alert("Duplicate Key " + message); 
     return; 
    } 

} 
} 

服務

public static Composer createComposer(String name) throws Throwable { 
    EntityManager entityManager = entityManager(); 
    Composer comp = new Composer(); 

    try { 
     comp.setName(name); 
     comp.setId(1); 

     entityManager.getTransaction().begin(); 
     entityManager.persist(comp); 
     entityManager.getTransaction().commit(); 

    } catch (Exception e) { 

     log.error("Error in Composer::createComposer(" + name + ") //" 
       + e.toString()); 
     throw e; 
    } finally { 
     entityManager.close(); 
    } 
    return comp; 
} 

我發現的一個問題是變量'ServerFailure失敗'只包含failure.message中的信息;所有其他變量都是空的。

相關問題