InfoQ

News

ORM with JRuby - ActiveHibernate

Posted by Werner Schuster on Aug 16, 2007 09:30 AM

Community
Java,
Ruby
Topics
JRuby ,
Ruby on Rails
Tags
ActiveRecord ,
Hibernate ,
JRuby ,
nHibernate ,
ORM
Object Relational Mapping (ORM) is a big topic with OOP languages. As soon as some data needs to be persisted, the question of how to store it comes up. One popular ORM solution in the Java world now gets support in JRuby. Kicked off by a blog post by JRuby team member Ola Bini, the ActiveHibernate project aims to make Hibernate available for JRuby developers. We talked to Johan Andries, consultant at AE, who is implementing ActiveHibernate.

Hibernate is popular in the Java space, and, with NHibernate, with .NET. Johan explains the typical user for ActiveHibernate:
We have developers in mind that want the productivity and other advantages of Rails, but that already have invested in a Java domain model or for some reason need more than Active Record. Examples are composite keys, legacy databases, prepared statements, complex class hierarchy mappings or Hibernate's first and second level cache. To make that possible we will have to provide an interface that follows the active record pattern without compromising access to the full power of Hibernate.

Initial focus was on mapping Ruby objects. Charles Nutter suggested that existing Java classes should be kept in mind as well. At its bare minimum you can write pure Ruby classes with attribute accessors and create the usual Hibernate mapping files by hand. There are already some tests in the Subversion repository showing just that. Working with the XML mapping files directly is obviously not very Ruby-like, so a DSL is underway.

This brings the topic to the question of how to define mappings with ActiveHibernate. Ruby lends itself well to embedded Domain Specific Languages (DSL), which allow to write Ruby code that resembles a more domain specific language instead of general code. (To learn more about DSLs, see this InfoQ presentation by Obie Fernandez on the topic of DSLs).

In the case of ActiveHibernate, it would be useful to have a DSL that allows to configure the mapping from Ruby or Java types to entries in a relational database table. In Java, this is done with XML files. Johan explains how this would look in Ruby:
There are two DSL-like things we can (and will) do. One form embeds the mapping information in the Ruby class definition. Ola Bini (who else?) has sent me a patch with some (meta)programming magic, which allows to write things like this:
class Project  
include Hibernate
 table_name = "PROJECTS" #optional
primary_key_accessor :id, :long
 # column names are optional
 hattr_accessor :date, :timestamp, :START_DATE
 hattr_accessor :name, :string
 hattr_accessor :complexity, :double
hattr_accessor :size, :long
hattr_accessor :on_schedule, :boolean
end

By including the Hibernate module (as opposed to inheriting from a framework base class), the Hibernate mapping is generated and configured automatically. Utility class methods (like "save" and "find") and accessor methods for the mapped properties are also added. Please note that this is a first working draft and we still welcome suggestions. This code will be in the repository real soon.
The other form keeps the mapping info separate from the Ruby (or Java) classes to keep these completely peristence ignorant. No work has been done on that yet.
Johan explains what would be involved for a port of ActiveHibernate to .NET, IronRuby and NHibernate:
The IronRuby/NHibernate combination seems like the most natural candidate. IronRuby is still in pre-alpha, but from what I have seen it will be possible to do something equivalent to the JRuby Java extension we did for Activehibernate. The NHibernate part, however, will be harder. NHibernate 1.2 is based on Hibernate 2, with a lot of Hibernate 3 goodies ported back. Unfortunately ActiveHibernate is based on new Hibernate 3 features (e.g. tuplizers, added to H3 to support dynamic maps and XML serialization) that haven't been ported to NHibernate (yet?). So it all depends on the roadmap for NHibernate. Anyway, it will be possible to share almost all the Ruby code that makes up ActiveHibernate (in the case we'd do a port).

Finally, Johan details the plans for the ActiveHibernate project:
ActiveHibernate is something I do in the evening, but I guess we should have something really usable and complete within one or two months time. Of course, it could go much faster if other people join in with ideas and patches (like Ola already did). People can already check out the code on http://code.google.com/p/activehibernate There's a readme file and a Rakefile, so that you can build and run the tests without much hassle. (But please keep in mind it is still early days.)
Johan also blogs about the progress of ActiveHibernate.
gorm? by serge boulay Posted Aug 16, 2007 11:03 AM
Re: gorm? by johan andries Posted Aug 18, 2007 8:13 AM
  1. Back to top

    gorm?

    Aug 16, 2007 11:03 AM by serge boulay

    this sounds alot like Gorm for groovy/grails. They are also (maybe done) creating a dsl for mappings and the ability to use JPA under gorm. They have a fantastic approach, maybe it can provide you with some ideas.

  2. Back to top

    Re: gorm?

    Aug 18, 2007 8:13 AM by johan andries

    Hi Serge, Gorm is indeed something to look at. Johan.

Educational Content

Bindings, Platforms, and Innovation

This presentation focuses on the Internet and separating myth from fact, history from the future, and the mundane from the imaginative. Bob Frankston presents a vision of what could and should be.

Orchestrating Long Running Activities with JBoss / JBPM

This article explores the use of JBoss and jBPM to implement design solutions that effectively address the issue of orchestrating long running activities.

Neo4j - The Benefits of Graph Databases

This presentation covers the use of graph databases as an optimal solution for data that is difficult to fit in static tables, rapidly evolving data or data that has a lot of optional attributes.

Realistic about Risk: Software development with Real Options

This session introduces Real Options and shows how it can help in running your project. Real Options is a decision-making process that can be used to manage risk.

Communication Flexibility Using Bindings

This article discusses the use of bindings on services and references (including the instance of non-configured bindings) as the means to implement SCA communications in a Web and SOA environment.

Writing DSLs in Groovy

After a short introduction to DSLs, Scott Davis plays with the keyboard showing how to approach the creation of a DSL by typing working snippets of Groovy code that get executed.

Scaling Agile with C/ALM (Collaborative Application Lifecycle Management)

IBM Rational and InfoQ present, Scaling Agile with C/ALM, an eBook showing organizations how to become “finely tuned software delivery machines” by enabling team integration and scaling.

Concurrent Programming with Microsoft F#

Amanda Laucher presents a real life enterprise application written in F#. She shows actual code snippets, explaining design decisions and suggesting how to use some of the F# constructs.