Reputation: 886
I am currently working on a simple web application through Google App engine using GWT. It should be noted that this is my first attempt at such a task.
I have run into to following problem/dilema:
I have a simple Class (getters/setters and nothing more. For the sake of clarity I will refer to this Class as DataHolder
) and I want to make it persistent. To do so I have used JDO
which required me to add some annotations and more specifically add a Key
field to be used as the primary key.
The problem is that using the Key
class requires me to import com.google.appengine.api.datastore.Key
which is ok on the server side, but then I can't use DataHolder
on the client side, because GWT
doesn't allow it (as far as I know).
So I have created a sister Class ClientDataHolder
which is almost identical, though it doesn't have all the JDO
annotations nor the Key
field.
Now this actually works but It feels like I'm doing something wrong. Using this approach would require maintaining two separate classes for each entity I wish to have.
So my question is: Is there a better way of doing this?
Thank you.
Upvotes: 4
Views: 1915
Reputation: 20920
You are correct in thinking that keeping two versions of your objects feels wrong -- the whole idea of GWT is that you can share your server-side objects on the client-side, and if you start divorcing the two you're not taking full advantage of GWT.
As to solving your problem, I've heard nothing but good things about Objectify, an alternate API into the datastore that's build just for App Engine. Among its many advantages, it's also GWT-safe, so you can pass Keys and everything back and forth between client and server. Read more here.
However, if you want to use JDO/JPA, you can just store your entity IDs as Strings or Longs, like so:
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class SomeDomainClass implements Serializable {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
String id;
}
Read more about that here (which is where I shamelessly stole that code example)
Upvotes: 2