JiBX: Hibernate for XML?

November 21, 2005

I'll admit, it isn't a perfect comparison, but there is no question that JiBX, one of the newer programs to enter the XML data binding space, certainly brings to the source code a lot of the same benefits as Hibernate. The fundamental similarity between these two frameworks is that they both allow you to develop classes following the common idiom of object-oriented design (POJO classes that use associations, inheritance, polymorphism, composition and collections). The mantra of the JiBX project is that data binding should be fast and data binding frameworks should allow for the divergence and evolution of the domain model from its XML storage mechanism. Sure sounds a lot like the O/R mapping portion of Hibernate, doesn't it?

Before I continue, I have to just get something off my chest. I loathe code generation! It just smells. In most cases it ends up bleeding all over your classpath with layers and layers of abstracted objects. XMLBeans is especially notorious for this problem. I want my day-eh-ta! Code generation often results in both code bloat and an overly complex data model for your application (thanks, in a large part, to the complexity of XML Schema). The application becomes tightly coupled with the XML schema and evidence of the domain model's XML heritage is propagated throughout all the layers of the application. In addition, you are forced to use the generated classes, which serve as no more than primitive information holders without any behavior. After beating my head against the screen trying to work around complications with using XMLBean generated classes inside of a JSF view, I finally hit the open road in search of a new data binding framework. It wasn't long before I came across JiBX, and I am now a true believer in mapped bindings for XML.

Mapped bindings (such as those implemented by JiBX) offer far more flexibility than code generation. You work with true object classes, which synthesize data and behavior. You can also decouple your object classes from the actual XML to a large extent. Modifying the mapping definition, rather than requiring changes in application code, often handles minor changes in the XML document structure. JiBX even supports the flattening of XML to conform to your data model, so a nested hierarchy in the document is not necessarily imposed on your domain structure.

When you need even more flexibility than the mapping behavior delivers out of the box, you can use either custom data converters for a single field or you can go so far as provide entire implementation classes to handle the marshalling and unmarshalling of the XML document. In the later case you are essentially providing a mapping implementation in Java rather than being confined to a fixed set of binding controls in the mapping file.

The best part about JiBX is that it opens the door for substitute persistent mechanisms down the road. For instance, if you are prototyping an application, or the requirements are narrow, you might decide that reading and writing data from an XML document is sufficient. However, if you do want to later move to an object-relational mapping framework such as JDO or Hibernate and begin persisting the data in a relational database, all that is necessary it to rewrite the mapping layer. The rest of the application should be ignorant to the fact that the method of saving and restoring data has changed.

While JiBX may be younger than some of the other data binding frameworks available, such as Castor and XMLBeans, it is decidedly complete. There is a plugin for Maven 2 and for Eclipse that makes configuring JiBX practically painless. With the growth of XML-based web services, dealing with XML data binding is becoming increasingly more crucial. The fact that JiBX is the most flexible AND the fastest framework on the web makes it a clear winner.

Posted at 10:44 PM in Java | Permalink Icon Permalink

14 Comments from the Peanut Gallery

1 | Posted by Andreas Brenk on November 23, 2005 at 01:15 PM EST

More information on the Maven 2 plugin is available at http://jibx.sourceforge.net/maven-jibx-plugin/.

2 | Posted by Brian McKendrick on March 09, 2006 at 01:03 AM EST

Personally, I have given up on these XML binding frameworks. Of course, if it really makes sense in the context of a project, I won't rule them out on principle. I just keep finding that I consider them less and less - I have been using the Jakarta Digester in their place in over a dozen projects and have been pleased with it's performance. Now - the Dihester really onky makes sense as long as the XML format is not overly complex and writing a bunch of bean classes can be a bit tedious .

3 | Posted by Dan Allen on March 10, 2006 at 08:24 AM EST

One of the reasons I really like JiBX is because it reminds me so much of the Apache Digester. However, opposite of you, I gave up on the Apache Digester because I found it overly complex (unless you enjoy thinking in stacks) compared to some of the binding frameworks, namely JiBX.

The most important aspect of any binding framework is that it is not intrusive. With JiBX, you can really sprinkle the data from your XML over your POJOs with very little effort.

4 | Posted by Bill Siggelkow on March 21, 2006 at 04:45 PM EST

Have you tried using Hibernate's ability to map directly to/from XML? Essentially using Hibernate as an XRM (XML-to-Relational mapping). My first impressions are quite positive.

5 | Posted by Dan Allen on March 22, 2006 at 12:12 PM EST

That's very interesting, I had no idea that was part of hibernate. However, I do see that there are two different needs arising here to map XML data to POJOs. It is summarized nicely in the following quote from an article I found online:

...understanding the Hibernate project's mission is important. Even though the Hibernate 3 XML features are highly useful and attractive, they are not meant to replace the most popular XML marshalling or transformation frameworks. Despite a very comprehensive OR mapping solution, Hibernate is not expected to grow into a major XML manipulation framework (per Hibernate author Gavin King, TheServerSide Java Symposium 2005).

Hibernate's XML is good if you want to marshall XML to and from a database of relational data. It isn't necessarily appropriate when your storage format is XML and a database plays no part (at least you would be bending it a bit).

6 | Posted by Brian on July 03, 2006 at 11:46 AM EST

I have used hibernate's 3.0 direct xml to relational data mappings. Take my advice, don't waste your time with it. Don’t listen to the think-tank sandbox Java hackers-cum-Enterprise Architects. Don't be seduced by the simplicity of this approach, of using one set of mappings files for pojos and xml. You will do neither side justice.

Hibernate's xml to relational data handling is grossly incomplete, the hibernate developers are not answering any questions in the user forums about xml handling, and Hibernate has not done any xml related improvements in the last 3 release candidates of hibernate 3.

One major weakness with hibernate's xml handling is with its treatment of inserting complex data with parent/child relationships. In this scenario, you would expect the JDBC semantics to be something like, an insert of the parent, then an insert of the child with the generated parent id. That is how it works on the pojo side, but not in the xml side.

On the contrary, for reasons only comprehendible to the brain trust that is the hibernate development community, in the xml world, the parent object is inserted, than the child object is inserted, than the child is updated, setting the parent id. If you have any kind of referential integrity in your database, you are completely hosed. It is also at least 30% slower than pojos.

This is one of a host of problems with hibernate's implementation. Trust me, any one recommending using hibernate's dom4j entity mode has never used it in a realistic scenario.

7 | Posted by Slammer on December 29, 2006 at 06:48 AM EST

Brian, I am affraid you are right. It seemed to me like a perfect solution. Before that, I tried to serialize my hibernate managed business entities to XML using Xstream. But I ended up with hibernate-decorated XML output containing org.hibernate.collection.PersistentSet and false tags.

Because I annotate my business entities I don't have .hbm.xml mapping files, so I can't even use the dom4j entity mode. (Read my post on: http://forum.hibernate.org/viewtopic.php?t=969080 )

Anyone has a good solution to convert your hibernate managed objects to XML?

8 | Posted by naitandu on February 16, 2007 at 08:41 AM EST


you can use Beanlib to get rid of all Hibernate-enhanced classes and initialize all lazy-connections. In Addition, you can configure Beanlib by code and decide which connections not to initialize...

9 | Posted by Luxspes on April 13, 2007 at 03:52 PM EST

And what about https://hyperjaxb2.dev.java.net/? have you tried it?

10 | Posted by Bill Pattchen on May 25, 2007 at 08:12 AM EST

Does JIBX support foreign keys in the schema definition?

11 | Posted by Dan Allen on May 25, 2007 at 08:39 AM EST

That I honestly do not know. I would encourage you to post that question on the JiBX user list to see if you can get some help there.

12 | Posted by Bill Pattchen on May 25, 2007 at 04:30 PM EST

after a day of research,I discover that it supports it.I just have to use id attribute with value "def" for primary key and "ref" for foreign key. Now i would like to know if JiBX can help me to add/update/delete/retrieve data to/from an xml document without worrying about the underlying implementation(just like hibernate does with relational database)?


By the way,this article about the "WHY" of JiBX and how it is related to data binding and hibernate is really famous,the best i've found while surfing on the web.

13 | Posted by smlghst on July 18, 2007 at 02:37 AM EST

I have to import/ export data from one db to other, my protect use hibernate for the data access layer, my first thought was to use Hibernate XML data binding for load my domain objects to xml. How can i do that ? meens What technologies think I must use?

14 | Posted by marcus on November 26, 2007 at 12:38 PM EST

hibernate's xml implementation seems to throw lazy loading out the window - no matter what it tries to read all of an object's children when creating the xml document, obviously a problem for objects with many children. It's get-aroundable by creating separate mapping files, but ugh @ that. will look into jibx.