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
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.

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!

1 comment:

Berlin Brown Discussions said...

I am very interested in this as I am trying to avoid pure java.

I will check in every once in a while for further developments.