ActiveRecord and Hibernate - Friends or Foes?

Currently, I am in the nice situation to decide for myself with which framework I will build a demonstration portal. So, as I want to profit from the fast-paced development processes (at least the Ruby aficionados say so) and the lightweightiness of Rails, I decided to go the Rail(s)Way.

Problem is, that I have to use a legacy database in PostgreSQL where I am not allowed to tinker with the structure, but I am forced to insert data into the tables. The database was created using Hibernate, which means an overall database sequence is used to generate the unique IDs. Unfortunately my new pal ActiveRecord does not know anything about global Postgres sequences, so I had to figure out a way to use the hibernate sequence in a hack to get my unique IDs.

What I did was to use the hook before_validation_on_create in the ActiveRecord table model to create an ID. This looks like this:

[ruby]
class Term < ActiveRecord::Base
before_validation_on_create :generate_id

private
# use the hibernate sequence to generate the next id
def generate_id
self.id = Term.connection,execute(”select nextval(’hibernate_sequence’)”).result[0][0]
end
end
[/ruby]

No comments yet. Be the first.

Leave a reply

Warning: comments containing links will be eaten by our spam protection.