Thursday, September 8, 2011

Failed to load resource: the server responded with a status of 500 (Internal Server Error) when using GWT & Hibernate

Eloquently described by Bart on docbyte (here), a very big issue with GWT and Hibernate is that GWT only supports a small subset of Java classes to be emulated.  Hibernate often returns PersistentList, PersistentBag, ... instead of the regular ArrayList.  These Hibernate classes are not supported by GWT and cannot be returned to the client.

Putting it plainly, JavaScript serialization of these classes will fail.

The solution!?  Mirror your Hibernate objects using Data Transfer Objects, or use a thrid-party library, such as  Gilead (Previously known as Hibernate4GWT).

DTOs are a common design pattern.  I leave that explanation and example for a later post.

As the site states, Gilead stands for Generic Light Entity Adapter.  It continues to explain that the idea behind Gilead is very simple,  transform persistent entities back to simple Plain Old Java Object, by removing code instrumentation and replacing persistent collections with their regular counterpart.

But it's not particularly that simple.  As the Gilead site continues to explain, you cannot remove persistent information (stored in instrumentation) and expect your JPA implementation to work properly without it. Gilead has to store the persistent information before "neutralizing" persistent entities (the clone operation), and put it back when the object is sent back to JPA (merge operation). Gilead allows you to store this (simplified) information on the POJO itself or on server.

Keep an eye out for the Gilead example in the next post.

And that is it.  Enjoy cleaner code.

Monday, September 5, 2011

Creating GWT Overlays

Want to create GWT overlays?  Don't know what overlays are, or even if you need one?  A quick introduction to GWT overlays may be in order.

Overlays are an easy way to intertwine JavaScript objects with your Java objects in your GWT applciation.  This has a number of useful advantages, including leveraging code completion and re-factoring with your IDE.

Say, we start with the following JSON string, a representation of user data.

{ "firstName" : "Bo", "lastName" : "Doe" }

You'd like to be able to call user.getFirstName() or user.getLastName() on the user representation as you would using normal Java notation for accessing POJO values.


    1.  Create a User class... 
public class User extends JavaScriptObject {
    ...
}

User extends JavaScriptObject, a marker class that GWT uses to identify User as a JavaScript object.  Next you need a way to create this object and superimpose the object type, User, on your JSON string.


    2.  Create the JavaScriptObject from the JSON... 
public class User extends JavaScriptObject {
    ...
    public static native User create(String json) /*-{
        return eval('(' + json + ')');
    }-*/;
}

IMPORTANT! You should trust your source before evaluating JavaScript.

The string parameter json in your call to create() is your JSON string as shown above. eval will parse your JSON string, and produce a similar JavaScript object.  Lastly, you'll need some way to retrieve your data.


    3.  Add methods to access JSON values...
public class User extends JavaScriptObject {

    public final native String getFirstName() /*-{
        return this.firstName;
    }-*/;

    public final native String getLastName() /*-{
        return this.lastName;
    }-*/;

    public static native User create(String json) /*-{
        return eval('(' + json + ')');
    }-*/;
}

Notice the this reference inside getFirstName() and getLastName().  That this reference identifies the JavaScript object that's overlayed, so use it as you would in any other JavaScript.

And that is it.  Enjoy cleaner code.