GWT with JPA
Asked Answered
C

7

10

I'm trying to build database application using GWT 1.5.3. I use JPA annotations with my objects. It seems in hosted mode GWT's RPC works fine. But when I try to compile my app using GWT-compiler I get errors like: "The import javax.persistence cannot be resolved", "Entity cannot be resolved to a type". toplink-essentials.jar is already included in my project path. What settings else do I need to solve this problem?

Coricoriaceous answered 1/4, 2009 at 1:44 Comment(0)
C
1

Ok, I've found what I was missing. I needed to include jpa-annotations-source.jar in my GWT-compiler path in myapp-compile.cmd script (or in ant build file). By the way can anyone tell me the origin of this jpa-annotations-source.jar file?

Coricoriaceous answered 1/4, 2009 at 7:8 Comment(1)
Also, I should say that in GWT 1.5 one doesn't need to include the above jar. Instead just use production jar such as toplink-essentials.jar; of course pass it to your GWT-compiler to compilation succeed; and DO NOT write <inherits name='javax.Persistence'> in your app.gwt.xmlCoricoriaceous
N
9

You can use Gilead (http://sourceforge.net/projects/gilead/) library to seamlessly manage JPA entities with GWT.

Regards

Normalcy answered 7/4, 2009 at 19:39 Comment(0)
E
6

You need to include the source code for the JPA annotations in the build path for your GWT project. See here for more details:

http://code.google.com/p/google-web-toolkit/issues/detail?id=1830&can=1&q=jpa

Specifically this jar file which will fix your problem:

http://google-web-toolkit.googlecode.com/issues/attachment?aid=1475633892125294312&name=jpa-annotations-source.jar

Ezequieleziechiele answered 1/4, 2009 at 4:13 Comment(1)
The link has changed to google-web-toolkit.googlecode.com/issues/… . Should not depend on such link as they are periodically regenerated dynamically. Should google for "jpa-annotations-source.jar google-web-toolkit.googlecode.com/issues/attachment".Trousers
G
2

The general problem of the JPA and GWT is that GWT itself doesn't support fancy JPA classes, so you just do simple POJO persistent entities DTO that implements the java.io.Serializable and have simple JPA resource annotations. You need to create the entity classes in the scope of the GWT client either have it under the yourproject.client package or add them with

source path="client"

source path="folderOfYourEntities"

in the GWT project's YouProject.gwt.xml file. This will include the entity objects in the GWT client so they can used them on client side of the RPC as well. The DAO must be on the server side and can contain anything that you container supports.

The problem you have now is that when compiling, GWT compiler saids that it desn't know what those imports for JPA annonations are in the entity DTO classes. That is why you need the javax.persistence class and source codes. The jpa-annotation-source.jar reference by Rustmyself works. It is just the javax.persistence compiled class files and source codes files plus a Persistence.gwt.xml. It is a simple GWT module for the javax.persistence package. If you know how to make your own GWT module, you should have problem making all this work. By the way, the official source for the Java EE can be found on the glassfish dev site's build section wiki.glassfish.java.net

There are many other solutions that wrap your fancy PU entities to simple objects automatically using proxy or to lazy load them at run time. They work, but not optimal solutions. The best practice is to make things simple and robust from the start by having POJO JPA DTO entities on the GWT client context and full blown DAO on the server.

GWTPersistence Example
I have added an actual working example on how to make GWT and JPA work seamlessly. It is a NetBean project with source codes and deployment file. See GWTPersistence on NingZhang.info

Gonophore answered 23/10, 2009 at 16:14 Comment(0)
C
1

Ok, I've found what I was missing. I needed to include jpa-annotations-source.jar in my GWT-compiler path in myapp-compile.cmd script (or in ant build file). By the way can anyone tell me the origin of this jpa-annotations-source.jar file?

Coricoriaceous answered 1/4, 2009 at 7:8 Comment(1)
Also, I should say that in GWT 1.5 one doesn't need to include the above jar. Instead just use production jar such as toplink-essentials.jar; of course pass it to your GWT-compiler to compilation succeed; and DO NOT write <inherits name='javax.Persistence'> in your app.gwt.xmlCoricoriaceous
A
1

I am also working with JPA <--> GWT data transformation etc.

In an effort to eliminate the DTO layer I used Gilead too.

My objection here is about translating javax.persistence. To avoid this I used XML JPA mapping declarations (orm.xml)

Alejandrinaalejandro answered 5/6, 2009 at 22:38 Comment(0)
R
1

Simply, keep another version of your Entities but without the annotations!

Remainder answered 27/12, 2010 at 1:32 Comment(0)
H
-1

Rebounding on synergetic's comment, you now (from GWT 1.5) only need to add

<source path='javax.persistence'/>

to your Module.gwt.xml

Hernando answered 6/2, 2013 at 15:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.