2

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

我在开发模式下没有错误,它编译得很好,但是当应用程序模块被加载时,我得到了错误。

我在 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(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?
4

2 回答 2

0

您根本不能在客户端 GWT 代码中使用EntityExistsException或之类的类型。EntityNotFoundException

这些是普通的 Java 类,GWT 不知道如何将它们转换为 JavaScript。

您只能在客户端代码中使用非常有限的部分外部库。这些库(例如Visualization)是专门为客户端设计和准备的,需要在应用程序的模块中继承它们的 GWT 模块。

我认为你真正想做的是这样的:

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

因为您可以将服务器端异常的类型读取为字符串,请参阅ReceiverServerFailure的 Javadoc 。

于 2012-06-02T12:09:29.507 回答
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 中的信息;所有其他变量为空。

于 2012-06-08T16:21:28.710 回答