【问题标题】:GWT+JPA Persistence.Exception source code not found未找到 GWT+JPA Persistence.Exception 源代码
【发布时间】:2012-06-09 00:21:28
【问题描述】:

我正在尝试使用 JPA 创建一个简单的数据库连接。 它工作正常,但是当我尝试向客户端抛出异常时,我得到了错误:

[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?

我在开发模式下没有错误,并且编译正常,但是当加载应用程序模块时,我得到了错误。

我在服务器/Composer 和客户端/Presenter 类中有所需的导入

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 在哪里找到源代码的任何想法?

谢谢

代码如下:

//从服务器中的Composer.class创建composer

    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;
}

///从 Presenter.class 中的 createComposer(above) 触发请求

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?

【问题讨论】:

    标签: gwt jpa persistence requestfactory


    【解决方案1】:

    您根本不能在客户端 GWT 代码中使用诸如 EntityExistsExceptionEntityNotFoundException 之类的类型。

    这些是普通的 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

    【讨论】:

      【解决方案2】:

      感谢 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”变量只包含 failure.message 中的信息;所有其他变量均为空。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-21
        • 2013-10-08
        • 2014-06-02
        • 1970-01-01
        • 2016-07-15
        • 2010-12-25
        • 1970-01-01
        相关资源
        最近更新 更多