ORMs and relational databases: powerful tools or dumb ideas?
A recent article on Object-Relational Mappers and the rising of non-relational databases has spawned a lot of comments on their benefits and drawbacks.
ORMs have been given many nicknames over the years, like the Vietnam of computer science. In the opinion of the author, ORMs are dumb, as it is a fundamentally broken idea to store an object graph into a relational database.
ORMs and ODMs
Are ORMs so unuseful? It seems that JBoss’s Hibernate, the first generic ORM worth its name, was a revolution for the Java world. To the point that the PHP open source community is following up by creating Doctrine 2 as an Hibernate for PHP.
It’s no mystery that sometimes ORM are a leaky abstraction and you’ll have to write a bit of SQL from time to time in critical points, to enhance their performance. But this is true also for every NoSQL store when used in companion of an object model.
For example, consider the MongoDB Object Document Mapper project started by Jonathan Wage, one of the core Doctrine developers. It is the equivalent of Doctrine, but it uses MongoDB, a non-relational database, as the target storage for an object graph. Here you have to do a mapping anyway, only it is performed by an ODM instead of an ORM.
How to persist an object graph
The only pure solution to store an object graph for persistence purposes is to do a periodical dump of the RAM (not always viable), or to run an object-oriented database as the primary storage mechanism.
Object-oriented databases raise lots of issues, apart from the poor support by hosting services. They’re specific to a programming language, and often violate the encapsulation of the single objects. Ideally, you shouldn’t be able to access private properties of objects to make queries on them.
It seems that an object-oriented model is not so fond of being persisted anyway: it is based on graph traversal local algorithms (and the logic of where to go in the graph is distributed in the methods of the various objects). Databases in general are instead based on declarative languages such as SQL or its ORM-spiced versions like the Hibernate or Doctrine Query Language.
By the way, being able to think in objects gives many advantages to a developer, mainly easiness of test, and freedom of modeling. Every PHP framework is object-oriented nowadays, even if PHP has historically been behind other languages in the adoption of this paradigm.
At the same time, databases are good at storing data and perform bulk operations on it directly. Every ORM allows to define UPDATE or DELETE queries which act on a large data set, but do not result necessarily in the reconstitution of a large part of the object graph.
Conclusions
Thus, ORMs meet many use cases for developers that want a level of abstraction over relational data stores and to avoid writing boilerplate code for data translation. Other types of Data Mappers suitable for NoSQL databases like MongoDb are growing. Do you think we will see the success or the failure of these solutions in the near future?
Leave a comment
Use the form below to leave a comment:
Responses and Pingbacks
August 20th, 2010 at 3:46 am
[…] danych a relacyjnymi bazami danych jest projekt mongoDB. Natknąłem się na niego czytając post “ORMs and relational databases: powerful tools or dumb ideas?”. Jak zawsze zachęcam do zapoznania się z materiałami na temat tego rozwiązania. Bliższe […]
August 30th, 2010 at 2:40 am
hey
November 30th, 2010 at 11:39 pm
The ORM concept is a great tool if you want to write all coding utilizing objects instead of arrays, even if you are storing the data in a normalized db. But if you are using ->fetchArray() or ending your data request with ->toArray() all the time, your are just P-ing in the wind and wasting memory and some cpu time.
April 29th, 2011 at 4:34 pm
ORM is not a substitution for proper SQL knowledge. And if you know SQl – IMO – why do you need ORM?
July 7th, 2013 at 1:41 pm
It seems to be a solution to the problem of developers being too lazy to just learn some SQL. Not to say there are some legit uses of it like with Magento’s EAV system.