首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >找不到GWT+JPA Persistence.Exception源代码

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

Stack Overflow用户
提问于 2012-06-02 08:06:57
回答 2查看 1K关注 0票数 2

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

代码语言:javascript
复制
[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类中有所需的导入

代码语言:javascript
复制
import javax.persistence.EntityExistsException;
import javax.persistence.EntityNotFoundException;

我还在类路径和构建路径中添加了以下jars:

javax.persistence.jar

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

我还尝试添加到gwt.xml

代码语言:javascript
复制
<source path='client'/>
<source path='shared'/>
<source path='server'/>

有关于如何告诉eclipse在哪里可以找到源代码的想法吗?

谢谢

代码如下:

//从服务端的Composer.class创建composer

代码语言:javascript
复制
    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的请求(上图)

代码语言:javascript
复制
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?
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-02 20:09:30

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

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

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

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

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2012-06-09 00:21:29

感谢Piotr的帮助。

下面是我最后做的代码:

客户端中的代码

代码语言:javascript
复制
req.fire(new Receiver<ComposerProxy>() {

                        public void onSuccess(ComposerProxy arg0) {

                            ComposerProxy comp;
                            comp = arg0;
                        }

                        public void onFailure(ServerFailure failure) {

                            serverError.getServerError(failure,
                                    "onAddButtonClicked");

                        }

                    });

我创建了一个类来处理错误

代码语言:javascript
复制
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;
    }

}
}

服务器中的服务

代码语言:javascript
复制
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中的信息;所有其他变量都为空。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10858772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档