views:

420

answers:

3

I have an Employee class

 @PersistenceCapable(identityType = IdentityType.APPLICATION)
    public class Employee {
        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Key key;

        @Persistent
        private String firstName;

        @Persistent
        private String lastName;

        @Persistent
        private Date hireDate;

        public Employee(String firstName, String lastName, Date hireDate) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.hireDate = hireDate;
        }

        // Accessors for the fields.  JDO doesn't use these, but your application does.

        public Key getKey() {
            return key;
        }

        public String getFirstName() {
            return firstName;
        } 
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        } 

        public String getLastName() {
            return lastName;
        } 
        public void setLastName(String lastName) {
            this.lastName = lastName;
        } 

        public Date getHireDate() {
            return hireDate;
        } 
        public void setHireDate(Date hireDate) {
            this.hireDate = hireDate;
        } 
    }

I have used the JDO for the app engine. Now I want to share this code between server and client. In which package should I keep this. In fact I have tried both way. Neither worked out. Please share if you have already done this type of codes.

A: 

I've done this before, but just in a small test app. Assuming you're using GWT-RPC, it should work pretty smoothly. You'll have to do two things:

  1. Put the code in a 'client' namespace, i.e. in a directory that's getting compiled by GWT. You can still use this code on the server.
  2. Hit compile and start fixing errors. The main one you'll find is that the 'Key' type isn't available in GWT land. You can use a string-encoded key instead. See the "key as encoded string" section in the relevant documentation.

If you're not using GWT-RPC, you're on your own. JSON is attractive for this purpose but requires significant legwork. This should be better in GWT 2.0 but won't entirely go away.

Russell Mull
This solved the compile time errors but But the SerializationException cant be solved in this way.
iftee
What SerializationException?
Russell Mull
+1  A: 

If what you are looking for is instantiating of your entities in both client and server, putting the classes under the "client" package will do the trick.

But if you are trying to pass your persistent entities through RPC, that probably wont work out of the box. DataNucleus "enhaces" the bytecode, and RPC can't serialize then. Hibernate has a similar problem, please take a look at this article, it explains the problem very well and presents alternatives.

I am creating DTOs to workaround this problem. It is a little more work, but it really depends on how many Entities you have.

ciczan
Thanks a lot for the link. I dont know, why I didnt get it on google search..
iftee
A: 

We probably need more detail, as you could be hitting a number of problems, but here's some tips:

  • The package doesn't matter so much as long as both the GWT compiler and javac can see it. I keep shared code in a package appropriately named... "shared". :)

  • Key is not available in GWT, so use an encoded string Key.

  • JDO is tricky, but workable. Newer versions of GWT (after Java AppEngine was released) have been able to handle DataNucleus' JDO enhancement. I'd make sure you are working off of trunk or a recent snapshot, in case DataNucleus is your problem.

  • Make sure you detach your objects before sending them to the client.

spankalee