Using entities with GWT + EJB + JPA
I'm using GWT(2.0)+EJB(3.x)+JPA deployed in GlassFish and using Eclipse Helios J2EE.
I have two entities in my EJB project: the Client and Provider entities. Using RPC I make a call from GWT to the EJB project which retrieves some entities and sends them back to the GWT project. The call is something like this:
GWT calls findClient from EJB开发者_开发技巧 which uses the EntityManager to find a Client entity and returns it to GWT. However when I try to compile my GWT project I get the following:
Compiling module com.MYPROJECT
Validating newly compiled units
[ERROR] Errors in 'file:/.....****....../GreetingService.java'
[ERROR] Line 20: No source code is available for type com.MYPROJECT.entities.Client; did you forget to inherit a required module?
[ERROR] Line 26: No source code is available for type com.MYPROJECT.entities.Provider; did you forget to inherit a required module?
[ERROR] Errors in 'file:/C:/workspaces/...***.../GreetingServiceAsync.java'
[ERROR] Line 14: No source code is available for type com.MYPROJECT.entities.Client; did you forget to inherit a required module?
[ERROR] Line 16: No source code is available for type com.MYPROJECT.entities.Provider; did you forget to inherit a required module?
The error message says it all, even though you might not realize it (I had the same issue). GWT compiles the Java code into JavaScript code and for that needs the source code. You are probably including the mentioned entities from another library and therefore the source code for those is not available, and therefore GWT can't do it's job.
What you (unfortunately) need to do is to create a separate object (within your project) and shuffle (i.e. move) the data from your entity to this new object.
You will have to make sure that you move the data in the correct way, i.e. something like the following:
static copyDataToGwtObject(MyObj obj) {
MyGwtObject gwtObj = new MyGwtObject();
gwtObj.setValueA(obj.getValueA());
gwtObj.setValueB(obj.getValueB());
}
What will (unfortunately) not work is to do the data moving in the constructor of the MyGwtObject :-(, i.e. don't do something like this:
class MyGwtObject {
private String valueA;
private String valueB;
public MyGwtObject(MyObj obj) {
this.valueA = obj.getValueA();
this.valueB = obj.getValueB();
}
}
This will not work, since GWT would (again) need the source code for the constructor.
Is your GreetingService
a RemoteService
? I use to "translate" the objects that the server returns into equivalent objects I created in the client project:
In GreetingSerciceImpl
(which extends RemoteServiceServlet
) I would write:
public TestClient someMethod() {
Client c = ejbClass.getClient() // returns object of Client class defined in the EJB project
return translate(c);
}
private TestClient translate(Client c) {
TestClient tc = new TestClient();
tc.setName(c.getName());
return tc;
}
Where TestClient
is defined somewhere in the client code of the GWT project.
HTH
精彩评论