GWT & Hibernate

Page 1

Using Hibernate with Google Web Toolkit (GWT) By Robert Green on November 12, 2007 5:09 PM | Permalink | Comments (4) | TrackBacks (0) Hibernate is an excellent tool which saves many projects from substantial amounts of code. Gone are the days of writing massive datasource layers full of obnoxious JDBC code. As gravy you also get simple query objects, caching, connection pooling and countless other great features. GWT on the other hand provides a robust API using a revolutionary new paradigm for writing javascript and complex dhtml interfaces. The latest version of Hibernate supports annotations, meaning you can ditch all of the XML that was previously required. This feature of course requires Java 5 to support annotations. Unfortunately, as of GWT 1.4, only Java 1.4 is supported on the client-side. I have created a solution to bridge the Java 1.4 to Java 5.0 gap. In my latest project, I started by creating the server-side code all using Hibernate 3 annotations. I didn't use a single XML file for configuration. Using pojo (Plain Old Java Object) entity classes annotated with EJB 3.0 / Hibernate compatible annotations, I had no problems building the relational object mapping and the core classes of the service. I had decided on trying GWT for the first time to develop the client-side code. I was able to integrate it into my development environment but immediately ran into an issue when I found out that there was no Java 5 support which is needed for Hibernate 3 annotations. What I ended up doing is creating a clientside class to work as the serializable, transient entity object. Yes, this does mean that I have both a serverside entity class and a client-side entity class, but think: All of your client-side GWT Java compiles into javascript anyway. Of course you'll need a javascript-based entity to work on the client-side. Example: Mapping two tables with a one-to-many relationship. The diagram below shows the server-side to client-side responsibilities. Each side has entities, but the server side does all the transformation between the two with the business logic. One could say that the client application requests the service which uses the business logic to process server-side entities and interface with the client-side with client-side entities. With that said, on to the code examples!

In my example we are modeling Shelfs, Books and the relationship between the two. What we have is a relational database with two tables: Shelf and Book. The Book table has a field with a foreign key reference to the Shelf's primary ID, creating a One-To-Many relationship of Shelfs to Books. In the real world this means that a shelf can have many books on it but a book can only be on one shelf at a time. On the server side, you will end up with some code that may look something like this:


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.