What is the correct approach to using GWT with persistent objects?

I am currently working on a simple web application through the Google App engine using GWT. It should be noted that this is my first attempt to complete such a task.

I ran into the following problem / dilemma:

I have a simple class (getters / seters and nothing more. For clarity, I will refer to this class as a DataHolder ), and I want to make it persistent. For this, I used JDO , which required me to add some annotations and, more specifically, add the Key field, which will 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 good on the server side, but then I can not use the DataHolder on the client side, because GWT do not let me ( as far as I know).

So, I created the sister class ClientDataHolder , which is almost identical, although it does not have all the JDO or Key annotations.

Now it works, but it seems like I'm doing something wrong. Using this approach will require the support of two separate classes for each object that I want to have.

So my question is: is there a better way to do this?

Thanks.

+4
source share
1 answer

You are right that you believe that saving two versions of your objects is not true - the whole idea of ​​GWT is that you can exchange your server objects on the client side, and if you start a divorce with two, not fully using GWT .

As for the solution to your problem, I haven’t heard anything but good things about Objectify , an alternative API in the data warehouse that builds just for App Engine. Among its many advantages, it is also GWT-safe, so you can transfer keys and everything back and forth between the client and server. Read more here .

However, if you want to use JDO / JPA, you can just save the object identifiers as strings or long strings, for example:

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

More on this here (where I shamelessly stole this sample code)

+2
source

Source: https://habr.com/ru/post/1305971/


All Articles