Me
About
Gallery
Company
Girl


Projects
Ruby on Rails
Basecamp
Highrise
Backpack
Campfire
Ta-da List
Writeboard


More
Feed
Archives

September 04, 11:38

Answering a hibernated cry for mercy

informIT just started a new multiple-part tutorial on how to use Hibernate (the top dog in Java ORMs) to create a domain model for a small book shop. By the end of the second XML file, I was ready to cry for mercy. So I did and Active Record answered:


class Book < ActiveRecord::Base
  belongs_to :publisher
  has_and_belongs_to_many :authors
end
 
class Publisher < ActiveRecord::Base
  has_many :books
end
 
class Author < ActiveRecord::Base
  has_and_belongs_to_many :books
end

Whups. I think we just got ahead of ourselves. The model above won't materialize in the Hibernate tutorial for another few installments. For the first one, they just got Book up and running.

But since it took all of five seconds to describe the domain (please note that were assuming the existence of a database schema, just like the Hibernate tutorial), let's give a few examples on its usage as well:


pruby = Book.create(
  "title" => "Programming Ruby", 
  "price" => "20", 
  "published_on" => Date.new(2004, 10, 3)
)
 
pruby.title # => "Programming Ruby"
 
dthomas = Author.create("name" => "Dave Thomas")
pruby.authors << dthomas
 
pruby.has_authors? # => true
pruby.authors.first.name # => "Dave Thomas"
 
pragshelf = Publisher.create(
  "name" => "Pragmatic Bookshelf")
 
pruby.publisher = pragshelf
pruby.publisher # => "Pragmatic Bookshelf"
 
publisher.has_books? # => true
publisher.books.size # => 1
 
# Say "Hi, Dave Thomas"
puts "Hi, " + publisher.books.first.author.name 

This is not pseudo-code. It's how most of the domain logic in Basecamp and other Rails applications work. Why does it how to be harder than this?

If you're ready to get outta dodge, please do have a look at Rails. It's a web-application framework that includes Active Record and its sister Action Pack. I wouldn't recommend it if you're trying to make a living as a tutorials writer and you're paid by the word, though.

Manual trackbacks: Rails: 1 vs Hibernate: 0


Challenge by jc on September 04, 16:47

David,

What are the mechanisms in ActiveRecord that allow for 'mapping' into an existing schema? ActiveRecord does look great, but all of the examples i've seen so far are of the kind where you are starting from scratch, which gives you the freedom of designing your own schema. However, in many cases either you have to work with an existing schema, or have the schema design designed by a DBA, to which you then have to map your domain to. Hibernate makes these kinds of scenarios simple to deal with (if you consider editing XML files and creating the mappings simple that is :)).

I agree that ActiveRecord is awsome to use, but i also find it a little too close to the database. For example, what happens when schema changes occour? The complexity of Hibernate can shield you from this.

I'm not trying to put down ActiveRecord and make Hibernate look good or vice versa. I think they are both great frameworks. I'm just trying to understand ActiveRecord a bit more as I am very impressed with that I have seen so far.

Thanks,
jc

Challenge by David Heinemeier Hansson on September 04, 17:00

Active Record is definitely geared towards new applications that has the freedom to design the database with non-business ID keys, proper foreign keys, and all the other hallmarks of a programmer-driven database design (vs a normalization/DBA one).

So that's where I see a clear advantage for Hibernate: Invigorate your crazy legacy database with a OOP shell. Active Record is certainly not aiming to storm the hill on that one.

That being said, it is actually possible to configure quiet a few things about the Active Record mapping. Like the name and type of the ID field, the table name that it's mapping up against, and more.

That's mostly to get people with strong feelings about whether table names should be in plural or singular on board with Active Record. Then when they've worked with the system for a while, they'll hopefully recognize that it's just easier to follow the preferred naming scheme and start to enjoy all the automation.

I call this "Conformity through freebies".

On schema changes: Why would you change your schema if you don't want the domain model to change with it? Outside the definition of new indexes (which Rails wouldn't care about), I can't come up with a good example (in the new app with App DB world).

Also, Rails is definitely geared towards the use of an Application Database rather than an Integration Database. Fowler has a good article on the differences.

Challenge by jc on September 04, 17:22

David,

Thanks for the prompt reply!

I understand the differences between Application Database and Integration Database, and now understand better where ActiveRecord fits in.

Challenge by Florian Weber on September 04, 17:25

i think basically it all comes down to one thing:

hibernate doesn't assume anything, its very flexible, so you have to specify everything you want it to do/know.

for so many cases though you don't need/want that flexibility. the per default assumption would work great.. and you rather specify the small things which differ from it, than specifying
everything from the ground up.

Challenge by Nathan on September 05, 6:53

Dude... relentless. Absolutely relentless.

I love it!

Challenge by on September 08, 16:56

Hibernate may look complicated but compared to other ORM technologies in Java it is simple and elegant (Have a look at an EJB persistance tutorial and then complain).

Challenge by David Heinemeier Hansson on September 08, 17:07

Oh, I think Hibernate is a marvelous piece of framework. I loved the book that Gavin and company wrote on it. Lots and lots of good ideas and a strong intellectual concept.

It's just that I find the "everything must be configured from scratch" approach to be way, way to laborious for my liking. All those configuration files and Java on top is just too much (for me).

Challenge by Keith Pitty on September 10, 4:46

David, I'm glad you have acknowledged Hibernate's "goodness".

The example you have given of using ActiveRecord is obviously trivial compared with many enterprise applications. And I'm sure you would acknowledge Hibernate's power as an ORM.

What limitations do you see with Active Record? For example, how does it handle mapping class inheritance and polymorphic associations?

Challenge by David Heinemeier Hansson on September 10, 10:56

Active Record is clearly most suitable for new application development. Hibernate has much more flexibility to allow mapping of legacy database systems with peculiar table definitions, so that's probably the most obvious limitation.

Active Record handles inheritance using Single Table Inheritance and polymorphic associations works just as you would think (has_many :people can return Person, Employee, or Manager as the type dictates).

Also, the example wasn't of my choosing. I just followed the domain as it was picked in the Hibernate tutorial.

Challenge by jarkko on September 20, 12:04

Sorry to be a late bird, but I'll have to bring still another Rails newbie opinion in play.

jc,

I don't quite see the problem of using an existing schema. If you take a look at the tutorial on Rails homepage, you can see that you can first design the schema and ActiveRecord then does the mapping automatically. I don't know why it would matter if the schema is created by you two minutes ago or by a DBA last year.

As for schema changes, I think that's where ruby as a dynamic language and ActiveRecord especially shine. If you change the schema (e.g. add field to a table), the AR object reflects that change automatically since AR builds the class dynamically. You can take that as a con (but, as David said, there's not many circumstances where you'd want to not reflect a schema change in the domain), or, like I do, a major productivity booster.

Of course there are changes that also need to be reflected in AR, most notably new associations. But the only thing that's needed is to say something like belongs_to :otherclass in the class definitions. That's far from repeating everything in an XML file.

OK, I don't want to ditch Hibernate in any way since I don't know much about it, and my experience even with ActiveRecord is very limited. But saying "inflexible" and "ActiveRecord" in the same sentence... nah, it really doesn't fit in with my experiences so far.

Challenge by george on June 04, 12:14

Hi. This is my links.
mature bitches mature anal older s3xy women mature anal mature amature classic mature mature nudists photos south indian mature women south indian mature women mature nud3 pics free thumbnail pictures of older women nud3 senior women nud3 senior women classic mature mature video south indian mature women mature ladies in lingerie mature anal mature nudists photos free thumbnail pictures of older women mature spunker mature living older moms older moms free thumbnail pictures of older women mature bitches older moms older s3xy women older women and younger men mature nudists photos classic mature nud3 senior women south indian mature women mature ladies in lingerie nud3 senior women mature spunker older moms south indian mature women lyrics to older country songs free thumbnail pictures of older women

Challenge by alex on June 04, 20:04

Hello. I am new here. It is very interesting.
free anal creampie movies creampie eating stories free creampie thumbnails creampie video store teen anal creampies creampies free creampie eating stories teen anal creampies creampie video clips free creampie video wife oral creampies creampie video clips wife oral creampies hairy creampie creampie pussies cum creampie creampies movies xyx creampies free anal creampie movies creampie video clips free creampie thumbnails creampie video store wife oral creampies creampie ass teen anal creampies creampies movies ass creampie free anal creampie movies creampie eating stories xyx creampies ass creampie hairy creampie cum creampie wife oral creampies creampie ass creampie eating stories xyx creampies creampie ass creampies free creampie video clips

Challenge by nik on June 05, 9:39

Hello. I am new here. It is very interesting.
creampie p0Rn free creampie pics free free anal creampie pics creampie internal, cumshots creampie anal interracial wives bareback creampies free creampie pics and stories creampie pussies amature creampies asian creampies pictures creampie latina creampie free creampie pics free free anal creampie pics free creampie s3x creampies xyx latina creampie free creampie s3x latina creampie creampie internal, cumshots creampies xyx creampie anal free creampie pics and stories gangbang creampies videos free creampie pics and stories free creampie pics and stories gangbang creampies videos creampie p0Rn creampie internal, cumshots free creampie s3x creampie pussies creampies xyx creampie pic creampie movie creampies free creampie cum and amature interracial wives bareback creampies free creampie clips pictures creampie asian creampies

Challenge by petra on June 05, 17:25

Hi. This is my links.
mature 1esbians free mature mature wives free older women mature men galleries mature babes mature men galleries older thumbs mature women nud3 old naked women free mature p0Rn mature amateur mature women nud3 mature over 50 mature asian mature men galleries mature nud3 women mature women t9p free mature mature 1esbians free mature s3x mature amateur mature asian mature women in stockings mature men galleries mature amateur mature babes older women younger men mature 1esbians old naked women mature nud3 women free older women mature men galleries mature over 50 free mature older women younger men mature nud3 women mature 1esbians mature amateur mature amateur

Challenge by irgy on June 06, 11:51

Hi. This is my links.
mature asian women thumbs gallery older women young girls older men mature pic gallery mature asian women mature hardcore links mature women over 40 older women stories mature glamour older holland mature nudists mature big tits mature asian women how to meet older women mature index mature index mature nudists young girls older men older 1esbians mature mistress older 1esbians black older ladies mature index young girls older men older holland older women stories mature mistress mature mistress older holland mature big tits mature index young girls older men mature mistress mature asian women black older ladies naked old women thumbs how to meet older women mature big tits mature big tits mature big tits

Challenge by den on June 08, 0:33

Hello. I am new here. It is very interesting.
satin panty galleries panty post mature pantyhose pics panty post white pantyhose wet pantyhose wet pantyhose free wet panty celebrities in pantyhose wet pantyhose girls in nylons men's pantyhose panty fetish men's pantyhose celebrities in pantyhose mature ladies in pantyhose panty thumbnails pantyhose images free wet panty panty fetish winnie-cooper pantyhose panty thumbnails satin panty galleries girls in nylons panty thumbnails free teen panties hooters girls in pantyhose girls in nylons men caught wearing pantyhose men caught wearing pantyhose cheerleader panty pics wet pantyhose free wet panty celebrities in pantyhose girls in nylons white pantyhose white pantyhose men's pantyhose pantyhose images wet pantyhose

Challenge by serge on June 08, 4:09

Hello. I am new here. It is very interesting.
free pantyhose sites free panties teens in pantyhose pantyhose foot gallery teen panty pictures teens in pantyhose pantyhose pic gallery crotchless pantyhose panty line pics crotchless pantyhose wolford pantyhose bra and panty pics mature women in nylons free pantyhose sites bra and panty pics shiny pantyhose panty line pics panty line pics wolford pantyhose pantyhose fetish free pantyhose pics free pantyhose sites free pantyhose pics free panties panty line pics shiny pantyhose free panties panty line pics free pantyhose pics panty line pics crotchless pantyhose pantyhose foot gallery free pantyhose pics pantyhose fetish teen panty pictures panty line pics men in panty pics free pantyhose pics free panties shiny pantyhose

Challenge by mikle on June 08, 19:23

Sorry for my links
top mature t9p's top mature t9p's victor mature meet older women daily mature boobs victor mature mature personals mature 1esbian victor mature free pics mature ladies meet older women top mature t9p's free mature movies free pics mature ladies meet older women meet older women free pics mature ladies daily mature boobs victor mature mature s3x pics mature content mature content free pics mature ladies mature content free pics mature ladies nud3 mature daily mature boobs free mature s3x pics mature content s3xy mature women s3xy mature women free pics mature ladies free mature s3x pics older pussy free mature s3x pics s3xy mature women free pics mature ladies mature 1esbian older pussy bcn mature