website stat

ORM: ActiveRecord vs Hibernate

While writing my last article I couldn't avoid comparing ActiveRecord and Hibernate. Although they apply different patterns in the Object Relational Mapping, they share the same fundamental purpous: mapping a relational database to objects and vice-versa.

Ads by AdGenta.com

Here's how AR stacks up with Hibernate in terms of number of lines of code.

SQL:
  1. CREATE TABLE miners (
  2.    id BIGINT NOT NULL AUTO_INCREMENT,
  3.    first_name VARCHAR(255),
  4.    last_name VARCHAR(255),
  5.    PRIMARY KEY (id)
  6. )

JAVA:
  1. /**
  2. * @hibernate.class table="miners"
  3. */
  4. public class Miner {
  5.     private Long id;
  6.     private String firstName;
  7.     private String lastName;
  8.     /**
  9.      * @hibernate.id generator-class="native"
  10.      */
  11.     public Long getId() { return id; }
  12.     public void setId(Long id) { this.id = id; }
  13.  
  14.     /**
  15.      * @hibernate.property column="first_name"
  16.      */
  17.     public String getFirstName() { return firstName; }
  18.     public void setFirstName(String firstName) { this.firstName = firstName; }
  19.  
  20.     /**
  21.      * @hibernate.property column="last_name"
  22.      */
  23.     public String getLastName() { return lastName; }
  24.     public void setLastName(String lastName) { this.lastName = lastName; }
  25. }

RUBY:
  1. class Miner <ActiveRecord::Base
  2. end

P.S. - This has improved in Java 6 but still...


5 Responses to “ORM: ActiveRecord vs Hibernate”

  1. Pedro Pais
    Published at February 9th, 2007 at 1:11 am

    That ActiveRecord stuff seems great… Why do you feel that Ruby (as many other languages) lack enterprise-wide adoption? Is it because it’s not supported by a big company, such as Sun or MS?

  2. mlopes
    Published at February 9th, 2007 at 1:17 am

    Well Pedro, I guess that there’s three major motives: first, it has just recently escaped from its hole thanks to Rails. It’s getting known and widely tested by the geeks and then by the enterprise.

    Secondly, there are still some myths around scalability. Despite the fact the in most of the cases the bottleneck lies on the database, Ruby has yet to prove itself in what concerns to scalability. Mongrel, LiteSpeed and ngnix are helping fixing this but it’s still vague.

    Thirdy, it lacks proper IDEs. If you recall, all the languages that enjoyed widespread enterprise adoption, Java, VB/.NET and Delphi, had great IDEs backing them up. Refactoring is absolutely crucial so is auto-complete. The lack of these functionalities has been a show-stopper in developing IMAGE.

  3. Pedro Pais
    Published at February 9th, 2007 at 2:07 am

    Thanks your for reply.

    Sooner or later you’ll have to uncover what that IMAGE project is all about! :)

  4. mlopes
    Published at February 9th, 2007 at 2:43 am

    Yeah, I’ll have to talk about it :-)

  5. mlopes
    Published at February 9th, 2007 at 11:48 pm

    I was unable in getting Wordpress to not remove the < / a > tag so I’ll try putting it here.

    Recommended reading: ActiveRecord vs Hibernate - The ORM showdown