Hyperjaxb3: XML to Java to Database (and back again)

Every now and again something comes along that just shifts the game along with a quantum leap. For me, the technologies that did this in the past were XML and Java Persistence (AKA Object Relational Mapping or ORM).

XML gave the world a standard language for describing pretty much anything, but until JAXB came along getting from Java objects to XML was a bit of a bind (no pun intended). Similarly SQL gave the world a standard(ish) way to talk to relational databases, but getting from Java objects to JDBC was also a bit of a chore – until JDO (and later JPA) came along to automate this process.

Recently though, as I have become more involved in implementing web-services, it became apparent that there was still a fair bit of manual labour involved if you wanted to go from client to database using XML and Java objects.

It is quite possible to mix JaxB and JPA by hand (tutorial here) but there are problems with this approach in my opinion.

  1. It’s usually code first and XML second.
  2. This makes the XML quite brittle and liable to change.
  3. Changing XML like this is hard for clients to keep up with.
  4. Generally it goes against the  ‘single semantic model’ approach advocated in SOA’s design principals.
  5. Generally it takes too long, and involves too much effort.

But most significantly, it’s quite tricky. You need a working knowledge of JPA and JAXB annotations and API’s to make it work. Skills like these are hard to acquire quickly and get in the way if you just want to get a project up and running.

However, a new java library from Aleksei Valikov called Hyperjaxb3 makes combining JPA and JAXB really easy. Hyperjaxb3 is basically an extension to JAXB that takes the objects generated by JAXB and annotates them so that they can also be used with JPA.

It’s a simple process which starts with the XSD of your object model and ends with database entries with Java objects being used in-between. Starting with XSD helps to maintain the integrity of your model, and prevents it mysteriously changing without your consent. Using JPA gives you a choice of Persistence Manager implementations including Eclipselink and Hibernate to name just a few.  The code generation process can be tweaked and tuned quite a bit to ensure that you get the object model that you want.

The results are fantastic. A proper ‘Soup-to-Nuts‘, XML to Database technology that adheres to standards supported by a number of enterprise class vendors.

It makes BEA ALDSP look very bloated and lethargic, particularly if you build Hyperjaxb3 into a Jee6 compliant RESTful web-service (like I have).