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.

No comments:

Post a Comment