I'm doing an experiment, developing an enterprise application in Java and Ruby in parallel, trying to evaluate whether Ruby is enterprise ready, or Java must still remain my weapon of choice.

Monday, December 11, 2006

Java & Hibernate vs. Ruby & ActiveRecord

I did a lot of work on our data model the past two weeks, a little with Ruby, a little with Java, and back again. I thought I should share some insights regarding the different approaches they take towards modeling your data, and their pros and cons.

In general, with Java and Hibernate you’re supposed to start with the objects, then add the relational mappings. With Ruby on Rails you start with the relational database, and ActiveRecord builds the object model automatically from that.

It makes perfect sense that each language chose the path it did:

Thinking in Java, it’s natural to start with the objects, carefully design and code your classes and interfaces, then add some XML or annotations (I used the latter) almost as an afterthought, just to enable persistence. Books and manuals treat the need to map between objects to a relational DBMS as a sort of nuisance that you have to deal with.

In Ruby, it not as natural to start by defining properties and their data types for your classes, which may have led to the acceptance of the idea to start with SQL DDL (that’s all about properties and data types), then do the rest with some ruby magic.

After discussing the respective approaches with some Java people I noticed a common perception which I think is a gross mistake. They tend to think that when you design a data model you should only think in objects, and that considering the relational persistence layer doesn’t happen at this stage, and thus the Rails approach is problematic. I think that’s wrong. After all, you have to start by thinking in terms of entities and their relationships. You can call these tables, objects or whatnot, but that’s exactly what you need for a relational model. It’s possible though that it’s just me – I did a lot of work with RDBMSs in the past, so my thinking is effortlessly geared towards the relational representation.

When it comes to simplicity and speed of development, ActiveRecord beats Hibernate hands down. I built my model in Rails (i.e. effectively in SQL) first, which is faster (for me) than defining Java classes. Going deeper into ActiveRecord in order to add the various data access methods I’ll be needing, and writing unit tests for everything took me less than two days. The process I had to go through in Java to achieve the same result has been going on for over a week, and I’m not done yet. Also, there’s far less

Note that in my case that included learning ActiveRecord, and learning Hibernate, EJB 3.0 O/R mapping annotations, HQL, and the various Spring mechanisms that wrap the Hibernate stuff. Much more learning to do on the Java side. On the other hand, I’ve a lot of experience with SQL. For someone with no SQL experience, the Rails part may take longer.

In short, the Ruby part was fast and easy, and thus much more fun.

The thing that annoyed me most with ActiveRecord are related to its lack of expressiveness: just by looking at the code, it’s impossible to tell what data members are hiding in my model - you have to look at the DB or migrations. Another issue is the conventions-based magic. The conventions for DB column names sure make things easy, but are rather annoying if you’re used to different conventions...

As usual, there’s a word of caution: all I did so far was very basic – I defined the model and added some rather naïve access methods. Rails tends to shine when you just start things up. Java and Hibernate may look better when my code gets more complicated.

For a more comprehensive comparison one (though outdated on some points) take a look at Patrick Peak’s Showdown article.