我正在尝试使用JPA创建一个简单的DB连接。它工作得很好,但当我试图向客户端抛出一个异常时,我得到了错误:
[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;我还在类路径和构建路径中添加了以下jars:
javax.persistence.jar
jpa-annotations soure.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的请求(上图)
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?发布于 2012-06-02 20:09:30
您根本不能在客户端GWT代码中使用诸如EntityExistsException或EntityNotFoundException之类的类型。
这些都是普通的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 Receiver和ServerFailure。
发布于 2012-06-09 00:21:29
感谢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中的信息;所有其他变量都为空。
https://stackoverflow.com/questions/10858772
复制相似问题