Header text

EssayTagger is a web-based tool to help teachers grade essays faster.
But it is not an auto-grader.

This blog will cover EssayTagger's latest feature updates as well as musings on
education, policy, innovation, and preserving teachers' sanity.

Wednesday, November 2, 2011

Spring + Hibernate on Google App Engine's new Cloud SQL!

Full project code for a Spring + Hibernate version of Google's "guestbook" sample webapp that accesses Cloud SQL!

Download the project code!

UPDATE: See the new version of my GenericDAO's save() method to avoid Session conflicts due to lazily-loaded member objects.

I was super-thrilled that Google approved my application to be part of the Cloud SQL limited preview. In gratitude I wanted to share the sample code that I used to do my proof-of-concept tests for accessing a SQL DB in App Engine.

I'm accustomed to using Hibernate with annotations for my ORM layer and was excited to be able to go back to that world in App Engine. But annotations won't work with App Engine as long as they limit the JPA spec to v1.x (I tried and failed to get around the limitation). So that means we're back to using Hibernate Tools and reveng.xml to generate the Hibernate *.hbm.xml mapping files. It's a pain and it feels like it's 2003 again, but whatever. Once you dust off that part of your memory it's not so bad.

I started with Google's "guestbook" sample app since that's what they use to step us through the basics of accessing Cloud SQL. Start there. Get their Cloud SQL-ized guestbook working first in your dev environment.

Then I decided that Hibernate works better through Spring. But I'm not using Spring for anything other than HibernateDaoSupport. The goal was to get Hibernate working, but with the minimum possible footprint. I think I've got it down to the bare essential jars and configuration.

Once the basics were working I retrofitted my Java generics-based approach to the DAO layer that I had built for the App Engine datastore with Objectify. It gets a little complicated, but basically there's only ONE class that's actually implementing all the standard DAO CRUD. Java generics do the rest to apply that code to each domain entity. It's pretty damn cool.

The approach is described in detail here:
(the above post also links out to David Chandler's original GenericDAO for Objectify that was the foundation for my generics layers)

The principles are all the same but the approach has been updated to be a bit slicker and more convenient (and, obviously, for Hibernate instead of Objectify). If the DAO/Manager layer of my Spring + Hibernate version of the guestbook app makes no sense, read that post.

I knew I wouldn't have time to write a fully detailed writeup so I did try to write a lot of comments in the code. Hopefully that'll answer most of your questions (especially related to the "Java magic" in my GuestbookManager class).

There's a db/ folder that contains a SQL CREATE script for my version of the guestbook database. The project assumes that you've done the setup described in the Cloud SQL docs (including the dev environment settings to access a local MySQL instance). Update the WEB-INF/config/database.properties file.

Access the sample webapp on your dev server at /guestbook

I checked my whole Eclipse project into the repository. My guess is that you usually keep those files out of a repo, so ignore them or delete them if they get in your way.

Final Thoughts
Thank you, App Engine team! Getting a MySQL instance integrated into App Engine is a huge, HUGE step forward!

I hope this sample code is helpful in getting other folks' Cloud SQL projects off the ground. And I am by no means an expert in all things Java; if you see a foolish decision or gaping hole in my code, please let me know!

Oh - and if this code is useful to you, help me out and tell all the teachers, professors, and TAs you know about my company, EssayTagger.com! We're putting all this technology together to make it easier and faster for teachers to grade essays--which is easily the worst part of the job. I would know, I'm also a public high school English teacher!

Download the project code!