views:

398

answers:

3
+2  Q: 

ORM Inheritance

Has anyone really wanted and used inheritance support on ORM tools, and if yes which one do you think offers the best support?

Or is ORM Inheritance a "pie in the sky" concept?

+1  A: 

If you mean inheritance in the domain classes, I use it all the time with NHibernate and/or Castle ActiveRecord, they support the three mapping strategies:

Mauricio Scheffer
Thank you, I'll check them out.
Otávio Décio
+2  A: 

I like this question a lot. I've used ORM tools (Toplink, now eclipselink, Hibernate) for a while and I've always seen this as referenced in JPA documents but I've never really had a need for it. Basically my philosophy is the ORM is just there to prevent you from writing tedius code to pull out records for the database. That really is the huge timesaver and it prevents you from making stupid mistakes. Sure you can do fancy stuff with this, but why not save it for the controller (if you're following MVC) than stick it in the model?

Greg
So would you say the traditional "one class per table" approach works for most cases?
Otávio Décio
In my case that's what I've always found. Using inheritance there just confuses the mapping IMO and is more of a toy than anything useful.
Greg
+1  A: 

I have used inheritance with Hibernate (and some with Django), and regreted it dearly.

The "composition over inheritance" principle is especially true for domain classes. While I agree that there is a few cases where inheritance makes sense at the model level, in most cases inheritance will give you a very static domain model, where one object will not be able to change to another class.

I also find that most developers are not comfortable with the concept of inheritance at the database level, so maintenance becomes more complicated.

And last, there are some technical problems, like proxies put in place by Hibernate that will hide the actuall class of an object. It makes "instance of" behave stangely. Of course, you might say that "instance of" is a code smell and that maybe it is another hint that composition is probably a better solution ...

Guillaume
Looks like a straight class-per-table (with composition) is the way to go from what I'm reading here. Thanks for the insight.
Otávio Décio
don't agree. If you keep in mind that you can't change an entity from one class to another, this is a very powerful technique as it lets you apply your common OOP knowledge to an otherwise relational model.
Mauricio Scheffer
As for checking the actual type behind a proxy, it's VERY smelly... use a visitor or polymorphism: http://www.hibernate.org/280.html
Mauricio Scheffer
well composition over inheritance is common OOP knowledge too, of course, but sometimes inheritance is more convenient (oh no, I hear a flame war coming! ;-) ) Anyway, it's another tool you can (or not) use.
Mauricio Scheffer
@mausch - If you were to use an ORM tool, would you consider inheritance "indispensable to have" or just "nice to have"?
Otávio Décio
It's certainly not indispensable, but *very* nice to have. It enables you to do polymorphic queries, see this example: http://metaarchit.com/hibernate_tutorials/Hibernate%20Tutorial%2008.pdf
Mauricio Scheffer
I would agree that it is a nice to have, but you have to be carefull not to over use it.
Guillaume