Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ORM and Antipatterns

 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill,

Does using an ORM such as Hibernate encourage the use of antipatterns? Or does the object oriented query approach helps reduce the number of antipatterns?

Kind regards,
Wouter
 
Bill Karwin
author
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Wouter,

Keep in mind that the value of ORM tools is to improve developer productivity, not to produce good SQL code. In a project with budget or time constraints (same thing, really), the largest expense is paying developers' wages, so ORM tools are successful if they reduce development time. That's kind of orthogonal with respect to SQL antipatterns.

But I can think of at least one antipattern that Hibernate is guilty of: Polymorphic Associations. This encourages developers to design tables where a foreign key can reference any of several parent tables. But to achieve this, you have to forgo using a foreign key constraint to enforce the references. That should be a red flag right there that Polymorphic Associations violates database design principles.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your fast response and taking the time to answer questions from us.

// Edit: typo
 
Gerardo Tasistro
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill Karwin wrote:
But I can think of at least one antipattern that Hibernate is guilty of: Polymorphic Associations. This encourages developers to design tables where a foreign key can reference any of several parent tables. But to achieve this, you have to forgo using a foreign key constraint to enforce the references. That should be a red flag right there that Polymorphic Associations violates database design principles.


You can still use only one table to store all classes. A column is added that identifies the type and thus allows the foreign key to reference only one table and not many as is the case of one table per class.
 
Bill Karwin
author
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gerardo Tasistro wrote:A column is added that identifies the type and thus allows the foreign key to reference only one table and not many as is the case of one table per class.


Yes, that's the common way to employ polymorphic associations: add another column for the "type" of entity you're referencing. For example:

The problem is that you can't declare a foreign key constraint to enforce referential integrity for polymorphic associations. A foreign key must reference exactly one parent table. So the only way to use polymorphic associations is to forgo referential integrity.

When we try to use an RDBMS in a way that makes it impossible to use a fundamental feature like referential integrity, this is a strong antipattern smell.
 
Gerardo Tasistro
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill Karwin wrote:
Gerardo Tasistro wrote:A column is added that identifies the type and thus allows the foreign key to reference only one table and not many as is the case of one table per class.


Yes, that's the common way to employ polymorphic associations: add another column for the "type" of entity you're referencing. For example:

The problem is that you can't declare a foreign key constraint to enforce referential integrity for polymorphic associations.


Example of MySQL database structure generated by Hibernate for a polymorphic representation of discounts in an application. There are various types of discounts that are represented in one table. Thus allowing many classes to reside on the same table and be referenced.

Personally I'm no fan of one table per class approach due to the reasons you mention. But using polymorphism on one table is very viable without breaking constraints.


 
Bill Karwin
author
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I admit I may have misunderstood Hibernate, it appears that it transparently creates a table to serve as a supertype. Mea culpa!
http://docs.jboss.org/hibernate/stable/core/reference/en/html/inheritance.html

The antipattern is when a child table references several parent tables using a single foreign key column, but there is no table for the supertype common to all the parents. It's more common for programmers who use loosely typed languages like PHP or Ruby to try to use the foreign key in the spirit of "duck typing."
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic