找不到GWT + JPA Persistence.Exception源代码

时间:2012-06-02 00:06:57

标签: gwt jpa persistence requestfactory

我正在尝试使用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?

我在开发模式下没有出错并且编译得很好,但是当加载app模块时,我就会收到错误。

我在server / Composer和client / 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创建作曲家
    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(上面)的请求

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?

2 个答案:

答案 0 :(得分:0)

您根本不能在客户端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")){
       ...
    }
}

因为您可以将服务器端异常的类型读作String,请参阅Javadoc for ReceiverServerFailure

答案 1 :(得分: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'只包含failure.message中的信息;所有其他变量都为空。