Friday, November 23, 2007

JRuby (and ActiveHibernate) at JavaPolis

It's nice to see that JRuby is getting the attention it deserves at JavaPolis:

And, while on the topic of ActiveHibernate, I have added support for lazy loading of entities and support for Hibernate components (aka aggregation in Rails):

class Address
attr_accessor :street
attr_accessor :city

class Person
include Hibernate
primary_key_accessor :id, :long
h_component :address,[[:street,:string],[:city,:string]]

I'll use the time that's left before JavaPolis to polish (i.e., clean up and extend the tests) the functionality that is in there today.

Anyway, I'm looking forward to meeting the JRuby team in Antwerp.

Sunday, November 4, 2007

.NET Web development without ASP.NET

A couple of weeks ago, Scott Guthrie and Scott Hanselman showed the first bits of Microsoft's upcoming MVC framework for web development at the ALT.NET conference. It is amazing to see how much of ASP.NET they actually ditched to come up with a real MVC framework. Their work seems very nice (fully plugable, dependency injection, ...), and it actually looks remarkably well like MonoRail or ProMesh.NET.

My current project switched from ASP.NET to ProMesh some time ago (which was not an easy decision given ASP.NET's omnipresence), but this evolution proves it was the right thing to do.

Tuesday, October 30, 2007

ActiveHibernate update

Just added basic many-to-one support in the mapping DSL. I still hope to have a JRuby on Rails application running with ActiveHibernate before Javapolis.

Update:I'll be doing a Javapolis Quicky on ActiveHibernate. In the meanwhile one-to-many (has-many in ActiveRecord speak) has been added to the mapping DSL example.

Tuesday, September 25, 2007

To DDD or not to DDD

Imagine the application you're currently working on. Imagine your users were happy with a primitive text based UI in a console, and imagine persistence was not an issue because you have an obscene amount of RAM (a billion terrabyte) and your server never crashes (so no DBMS needed).

Would there be any complexity left to deal with in your software? If the answer is no, you're lucky (in a way). If the answer to this highly hypothetical question is still yes, then domain driven design (DDD) might be the thing for you.

Subclasses with ActiveHibernate

Finally I resumed my work on ActiveHibernate. It now uses a XmlMarkup Builder and the visitor pattern to sort out the relationships between different persistent classes and to generate the mapping XML internally. This might sound like overkill, but even to get inheritance up and running it seemed like the only way to keep things maintainable. Anyway, the Ruby code

class Payment
include Hibernate
hattr_accessor :amount,:double

class CreditCardPayment < Payment
join_with_table do
hattr_accessor :creditCardType,:string

class ChequePayment < Payment
hattr_accessor :chequeNumber,:string

generates the mapping

<class name="Payment" table="PAYMENT">
<id name="id" type="long" column="PAYMENT_ID">
<generator class="native"/>
<discriminator column="PAYMENT_TYPE" type="string"/>
<property name="amount" type="double"/>
<subclass name="CreditCardPayment" >
<join table="CREDIT_CARD_PAYMENT">
<key column="PAYMENT_ID"/>
<property name="creditCardType" type="string" />
<subclass name="ChequePayment" >
<property name="chequeNumber" type="string" />

The <join/> construct in the CreditCardPayment mapping allows to combine the table-per-class-hierarchy and the table-per-subclass mapping strategies in one inheritance hierarchy. In the Ruby mapping DSL it can be nicely expressed with a class method (table name and key are optional arguments) and a block. The fact that a Ruby class definition is just ordinary Ruby code (with self referring to a Class object) opens a whole new world to the simple Java developer that I am.

In Subversion soon.

Tuesday, September 11, 2007

Deep Cloning an Object Graph in .NET

The other day I was looking at the really nice NHibernate mapping examples by Davy Brion. His entity base class looks like this:

public abstract class Entity
private long _id = -1;

public virtual long Id
get { return _id; }

public virtual T Clone()
MemoryStream memoryStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, this);
memoryStream.Position = 0;
return (T)binaryFormatter.Deserialize(memoryStream);

It is used like so:

public class Order : Entity<Order>

The Order class derives from a template class that uses the Order class itself as parameter. I had never seen this before, and, for example, it gives all your entities a Clone() method that returns the proper type.

What's even more interesting is the way the Clone() method has been implemented by just serializing to a memory stream, immediately followed by a deserialization. It gives you a deep clone of your entity, together with all child entities it references. This technique is robust, simple, clean and generic (requiring no extra development effort).

I can imagine some people will object to this because of performance, but that only makes me think of all the crimes that are committed daily in the name of performance and (premature) optimization.

Monday, August 27, 2007

ActiveHibernate follow-up

Not much happened the last couple of weeks because it was our summer vacation (which summer?). I have cleaned up the existing code a bit, and now I'm working on a Ruby way for Hibernate inheritance mapping.

Wednesday, August 15, 2007

Towards a mapping DSL for AH

Last week Ola Bini sent me a patch that showed a Hibernate mapping written inside the Ruby class definition, instead of relying on hand-written XML files (the horror!), like so:

class Project
include Hibernate

with_table_name "PROJECTS" #optional

primary_key_accessor :id,:long,:PROJECT_ID
hattr_accessor :date,:timestamp,:START_DATE
#column name is optional
hattr_accessor :name,:string
hattr_accessor :complexity,:double
hattr_accessor :size,:long
hattr_accessor :on_time,:boolean

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.

It's all in the file dsl_test.rb (see the link in the top right corner for the code repository). It is far from complete, but thanks to Ola I can start filling the gaps (like inheritance, relations, components, ...). Let me know what you think!

Monday, August 6, 2007

First ActiveHibernate code on Google Code

Because it's all talk until the code runs, I have finally imported my code into a Google Code project ( There is no formal release yet, so you'll have to use a Subversion client to get it (or just browse the repository online). There's a readme file that explains how to get the tests running.

All feedback is more than welcome, as I'm new to (J)Ruby. I'm also very interested in ideas for a Ruby-like interface to Hibernate. The first thing that should be done is replacing the hbm.xml mapping files by a kind of annotations or a DSL (inspired by Rails or GORM, the OR/M in Grails).

Friday, July 27, 2007

ActiveHibernate: time to package

Good news. Hibernate collection mapping is up and running. Well, one example that is: Hibernate's bag type now works with Ruby Array's, lazy loading included. To get started I've done a shameless copy/paste of the Hibernate code for the bag collection type. That went fairly easy because the Java class RubyArray implements the java.util.List interface. So now the mapping files look like


<class table="BLOGS" lazy="false" entity-name="Blog">

<tuplizer entity-mode="dynamic-map"

<id name="id" column="BLOG_ID" type="long">
<generator class="identity"/>

<property name="title" type="string" />

<bag name="items"
<key column="blog_id" />
<one-to-many entity-name="Item"/>



where Blog and Item are genuine Ruby classes. The nice part is that I haven't touched any existing Hibernate or JRuby code.

The plan is to package the things I already have (still need to write more tests to show what is working) and put it on SourceForge or Google Code to get some feedback from JRuby and/or Hibernate people. (I can very well imagine I've made things to complex where simpler solutions exist out-of-the-box.) After that it's time to start looking at ways to get rid of the XML mapping files by writing a friendly Ruby interface around the Hibernate details.

I hope that after some time this project will be stable/useful enough to be part of JRuby Extras at RubyForge.

Monday, July 23, 2007

ActiveHibernate: First Steps

Things are going smoother than I expected. Hibernate 3 already supports the use of dynamic maps and XML documents (these are alternative entity-mode's in Hibernate lingo) through the tuplizer mechanism. By extending Hibernate's AbstractEntityTuplizer class it is remarkably simple to let Hibernate work directly with instances of IRubyObject. Features like inheritance and many-to-one relations seem to work fine. (Haven't tried components and proxies yet.)

Now I'm trying to get collection mapping working (one-to-many). Hibernate wraps the user's collection (which would be a Ruby Array in our case) by its own implementation (of some java.util collection interface like java.util.List when dealing with POJOs) to track additions/removals and to support lazy collection loading. Customizing this is also nicely supported by Hibernate through user collection types. For this we need a wrapper that looks (walks, swims and quacks) like an Array to the Ruby code, and implements org.hibernate.collection.PersistentCollection to keep Hibernate happy on the Java side. I haven't found any other solution than to write a JRuby Java extension (by implementing JRuby's BasicLibraryService interface) to create a RubyObject subclass named PersistentRubyArray (to stay in line with the Hibernate naming convention) that wraps a RubyArray instance.

Anyway, it's fun to manipulate Ruby objects from within Java like puppets on a string, and to see how it all fits naturally with Hibernate.

Wednesday, July 18, 2007

JRuby + Hibernate = ActiveHibernate?

A couple of months ago, Ola Bini coined the idea of ActiveHibernate as an extra option when running Rails on JRuby (complementary to ActiveRecord-JDBC). After some debug sessions through the Hibernate and JRuby code I have convinced myself that this is indeed a nice and feasable project to start (or try to, at least). As Ola mentioned there are two parts to this:
  1. Configure/tweak Hibernate to let it work directly with the org.jruby.RubyObject class. I'm already impressed by the clean internal design of the Hibernate code, and the possibility to plug in custom implementations of Hibernate interfaces through the hbm.xml mapping files.
  2. Provide a Rubyish interface to the Hibernate configuration and functionality, following the fine Ruby on Rails tradition.
Point one seems the hardest to me, as I would like to keep the number of changes to org.hibernate (and org.jruby) code very low. None if possible (?). For the second point, ActiveRecord, Grails' O/R mapper (GORM) and Hibernate Annotations can serve as examples to learn from. I'm also looking forward to IronRuby to see if it could be married to NHibernate in a similar way.

As soon as I have something up and running I'll post the code to SourceForge, or RubyForge, or ... What would be the most appropriate hosting site for this kind of project?