Skip to content

Article about using hibernate with GWT

I just read this article about the Google Web Toolkit and hibernate, and I’m thrilled that someone wrote this. A few years ago, when I was just starting to use GWT and hibernate, the ORM tool, I thought about writing something similar myself. I could never get over the hump of writing about setting up all the infrastructure necessary, something which the author does quite nicely.

I think this article gives a great overview of some of the complexities of using hibernate with the GWT client. The author essentially talks about three possible solutions to the primary problem when using hibernate objects in a GWT system: hibernate enhances your POJO code, and thus you cannot send objects returned from hibernate queries down the wire to the JavaScript client.  The JRE emulation simply can’t handle it.

I especially enjoyed the explanations of how to use some of the tools, to make mapping between GWT capable objects and hibernate objects easier. I’d heard of hibernate4gwt, now Gilead, but never used it. For most of my RPC calls, I end up using the first approach the author explores, custom DTO creation. Often times, I won’t create a special DTO object, but rather reuse the POJO that represents the domain object. This way, you can scrub subsidiary objects (though you lose lazy loading when you do this) and send those down as well.  As long as the POJO doesn’t have too many extraneous members, this seems to work fine, and removes the need for an extra class.

I was a bit frustrated, however, that the author ignored the delete case. This seems like a situation where tools like Gilead might really shine. I have often run into issues where I have to add a ‘deleted’ boolean flag to the hibernate object.  I do this because when an object gets deleted from a collection on the GWT side, my server-side code has no way of knowing this, without some additional complexity (rerunning the query and doing a comparison of results). Adding such a ‘deleted’ boolean flag, solves one set of problems, but raises additional complexity, because you end up having to check to see whether or not an object exists before you try to insert it in the database.

For example, imagine you have a user with set of CDs, which you display in a grid.  If you want to allow a user to correct the name of one of the CDs, and send it back, the server side has the modified record, hopefully with and ID, and can simply save it.  But if you delete one of the CDs from the collection, the server side does not have the modified object, and so has to figure out which one to delete.  Gilead, with its knowledge of the object graph, seems at first glance like it could solve this problem elegantly (a quick search on the Gilead site shows nothing that I could see).

Also note that, using RPC is fantastic for GWT applications, but if you think about using GWT for widgets, I would suggest using something that gives you a bit more flexibility like JSONP. Because GWT RPC depends on XMLHTTPRequest, it is fundamentally limited to sites where the JavaScript and RPC services are on the same host.  Obviously, since using JSONP serializes hibernate objects to strings, none of these tools are appropriate.  (See my survey of Google Web Toolkit client-server communication strategies for more.)

All that said, if you’re thinking about using hibernate and GWT in the same project, reading this paper and running through the examples will be a worthwhile use of your time.

[tags]hibernate,gwt,useful articles[/tags]

8 thoughts on “Article about using hibernate with GWT

  1. Bruno Marchesson says:

    Hello Dan,

    Gilead handles deleted items properly, since it keeps a simplified version of the persistent collection (either on POJO or on server, depending on configuration).
    Furthermore, it uses the same kind of algorithm you describe : Domain classes are used to create neutral (unenhanced) DTO, without need of extra hierarchy.

    Hope this helps !

  2. Dan Moore says:

    Thanks Bruno, that’s great to know!

  3. raghava says:

    Thank’s for the info…..

  4. KA says:

    I’ve been working with GWT for 2 weeks only. I have a simple question: why cannot GWT be configured to translate to js/json hibernate classes like org.hibernate.collection.PersistentSet? or am I missing the obvious? in other words, configure xxx.get.xml so that it knows about hibernate classes and therefore GWT compiler should be able to translate these classes.


  5. moore says:

    Hi KA,

    It’s because those classes, like org.hibernate.collection.PersistentSet, would have to be re-implemented with classes in the JRE subset of GWT. So it’s not impossible, but would be hard to do (especially since the classes are a moving target–they’re changing too). I just looked at the source of PersistentSet and it has all kinds of classes that aren’t in the GWT JRE.

  6. KA says:

    Hi Moore,
    Thanks so much for your help! If I understood you correctly, any class that is referencing any class that is out side of GWT java.lang, java.lang.annotation, java.util,, java.sql needs to be rewritten so that it uses GWT JRE before it can be cross-compiled – is that correct?
    So, if my understanding is correct, let’s assume org.hibernate.collection.PersistentSet uses a class that is not in the GWT JRE, let’s call it NotGwtJreClass, then why cannot we include NotGwtJreClass in the xxx.gwt.xml so that it can be gwt-cross-compiled and so on so forth… We might end up with 100’s of classes/packages in the xxx.gwt.xml but it is still doable.

    Again, thanks so much for your help!

  7. moore says:

    Hi KA,

    In your example, you’d have to rewrite NotGwtJreClass, which might, say, depend on java.sql.ResultSet to either:
    * not depend on java.sql.ResultSet
    * use a GWT compatible java.sql.ResultSet.

    Since java.sql.ResultSet might be crucial to the functioning of NotGwtJreClass, you’d probably go the second route, which would mean you’d have to examine all dependencies of java.sql.ResultSet and re-write them, etc, etc.

    You can’t simply include the NotGwtJreClass because the GWT compiler will have no idea how to translate it into javascript, right?

  8. KA says:

    Thanks Moore for your help. I am going to try Gilead but I am a bit concerned with Gilead performance when there is big set of data to ship.

    Thanks again.

Comments are closed.