Search code examples
gwtjpapersistencerequestfactory

GWT+JPA Persistence.Exception source code not found


I'm trying to create a simple DB connection using JPA. It works fine but when I try to Throw an Exception to the client I get the error:

[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?

I get no error in development mode and it compiles fine, but when the app module is loaded there is where I get the error.

I have the required imports in server/Composer and client/Presenter classes

import javax.persistence.EntityExistsException;
import javax.persistence.EntityNotFoundException;

I also added the following jars to the classpath and build path:

javax.persistence.jar

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

I also tried adding to gwt.xml

<source path='client'/>
<source path='shared'/>
<source path='server'/>

Any ideas on how to tell eclipse where to find the source code??

Thanks

Here is the code:

//Create composer from Composer.class in server

    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;
}

///fire Request from createComposer(above) in Presenter.class

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?

Solution

  • You can't use types such as EntityExistsException or EntityNotFoundException in client-side GWT code at all.

    These are plain Java classes and GWT don't know how to translate them to JavaScript.

    You can only use very limited part of external libraries in your client-side code. These libraries (like Visualisation for example) are designed and prepared specifically for client-side and require inheriting their GWT module in your application's module.

    I think that what you really want to do is something like that:

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

    Because you can read type of server-side exception as String, see Javadoc for Receiver and ServerFailure.