Enterprise Java

ORM Haters Don’t Get It

I’ve seen tons of articles and comments (especially comments) that tell us how bad, crappy and wrong is the concept of ORM (object-relational mapping). Here are the usual claims, and my comments to them:
  • “they are slow” – there is some overhead in mapping, but it is nothing serious. Chances are you will have much slower pieces of code.
  • “they generate bad queries which hurts performance” – first, it generates better queries than the regular developer would write, and second – it generates bad queries if you use bad mappings
  • “they deprive you of control” – you are free to execute native queries
  • “you don’t need them, plain SQL and XDBC is fine” – no, but I’ll discuss this in the next paragraph
  • “they force you to have getters and setters which is bad” – your entities are simple value objects, and having setters/getters is fine there. More on this below
  • database upgrade is hard – there are a lot of tools around the ORMs that make schema transition easy. Many ORMs have these tools built-in
But why do you need an ORM in the first place? Assume you decided not to use one. You write your query and get the result back, in the form of a ResultSet (or whatever it looks like in the language you use). There you can access each column by its name. The result is a type unsafe map-like structure. But the rest of your system requires objects – your front-end components take objects, your service methods need objects as parameters, etc. These objects are simple value-objects, and exposing their state via getters is nothing wrong. They don’t have any logic that operates on their state, they are just used to transfer that state. If you are using a statically-typed language, you are most likely using objects rather than type-unsafe structures around your code, not to mention that these structures are database-access interfaces, and you wouldn’t have them in your front-end code. So then a brilliant idea comes to your mind – “I will create a value object and transfer everything from the result set to it. Now I have the data in an object, and I don’t need database-access specific interfaces to pass around in my code”. That’s a great step. But soon you realize that this is a repetitive task – you are creating a new object and manually, field by field, transferring the result from your SQL query to that object. And you devise some clever reflection utility that reads the object fields, assumes you have the same column names in the DB, reads the result set and populates the object. Well, guess what – ORMs have been doing the same thing for years and years now. I bet theirs are better and work in many scenarios that you don’t suspect you’ll need. (And I will just scratch the surface of how odd is the process of maintaining native queries – some put them in one huge text file (ugly), others put them inline (how can the DBAs optimize them now?))
To summarize the previous paragraph – you will create some sort of ORM in your project, but yours will suck more than anything out there, and you won’t admit it’s ORM.
This is a good place to mention an utility called commons-dbutils (Java). It is a simple tool to map database results to objects that covers the basic cases. It is not an ORM, but it does what an ORM does – maps the database to your objects. But there’s something missing in the basic column-to-field mapper, and that’s foreign keys and joins. With an ORM you can get the User’s address in an Address field even though a JOIN would be required to fetch it. That’s both a strength and a major weakness of ORMs. The *ToOne mappings are generally safe. But *ToMany collections can be very tricky, and they are very often misused. This is partly the fault of ORMs as they don’t warn you in any way about the consequences of mapping a collection of, say, all orders belonging to a company. You will never and must never need to access that collection, but you can map it. This is an argument I’ve never heard from ORM haters, because they didn’t get to this point.
So, are ORMs basically dbutils plus the evil and risky collection mapping? No, it gives you many extras, that you need. Dialects – you write your code in a database-agnostic way, and although you are probably not going to change your initially selected database vendor, it is much easier to use any database without every developer learning the culprits if its syntax. I’ve worked with MSSQL and Oracle, and I barely felt the pain in working with them. Another very, very important thing is caching. Would you execute the same query twice? I guess no, but if it happens to be in two separate methods invoked by a third method, it might be hard to catch, or hard to avoid. Here comes the session caching, and it saves you all duplicated queries to get some row (object) from the database. There is one more criticism to ORMs here – the session management is too complicated. I have mainly used JPA, so I can’t tell about others, but it is really tricky to get the session management right. It is all for very good reasons (the aforementioned cache, transaction management, lazy mappings, etc.), but it is still too complicated. You would need at least one person on the team that has a lot of experience with a particular ORM to set it up right.
But there’s also the 2nd level cache, which is significantly more important. This sort of thing is what allows services like facebook and twitter to exist – you stuff your rarely-changing data in (distributed) memory and instead of querying the database every time, you get the object from memory, which is many times faster. Why is this related to ORMs? Because the caching solution can usually be plugged into the ORM and you can store the very same objects that the ORM generated, in memory. This way caching becomes completely transparent to your database-access code, which keeps it simple and yet performant.
So, to summarize – ORMs are doing what you would need to do anyway, but it is almost certain that a framework that’s been around for 10 years is better than your homegrown mapper, and they are providing a lot of necessary and important extras on top of their core functionality. They also have two weak points (they both practically say “you need to know what you are doing”):
  • they are easy to misuse, which can lead to fetching huge, unnecessary results from the database. You can very easily create a crappy mapping which can slow down your application. Of course, it is your responsibility to have a good mapping, but ORMs don’t really give you a hand there
  • their session management is complicated, and although it is for very good reasons, it may require a very experienced person on the team to set things up properly
I’ve never seen these two being used as arguments against ORMs, whereas the wrong ones in the beginning of this article are used a lot, which leads me to believe that people raging against ORMs rarely know what they are talking about.
Reference: ORM Haters Don’t Get It from our JCG partner Bozhidar Bozhanov at the Bozho’s tech blog blog.

Bozhidar Bozhanov

Senior Java developer, one of the top stackoverflow users, fluent with Java and Java technology stacks - Spring, JPA, JavaEE, as well as Android, Scala and any framework you throw at him. creator of Computoser - an algorithmic music composer. Worked on telecom projects, e-government and large-scale online recruitment and navigation platforms.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

13 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
vernetto
vernetto
12 years ago

what is crappy is not ORM, but some of its implementations, like EclipseLink. I also lament that JPA provided a wrapper around some illustrious high quality implementations, like Hybernate, but removing some very valuable stuff (wrappers are usually worse than the thing they wrap, see also Sping’s Hybernate wrapper…)

Chris
12 years ago

Having used Hibernate and having taken a class in RDB, I found that I can get better performance from executing custom queries on views that I wrote than relying on Hibernate to build efficient queries.  Mind you, those queries were joining 4 or more tables and in one case, the schema had a foreign key to the primary key of the same table (self referential). My conclusion is that if you are building a something simple like a billing system then you really need Hibernate.   If you are doing anything even vaguely computational like natural language processing, then you… Read more »

Matt
Matt
10 years ago
Reply to  Chris

I know it’s an old comment, but you can map database views to classes using Hibernate. Processing stuff on the database it’s not a very good practice in the first place. Usually in large scale applications the database is the bottleneck in the application (and the most difficult thing to scale), the less thing it does the better. If you want to develop using an ORM you have to think a little different than when you develop with database in mind. I’m not saying that you have to disregard the database, I’m just saying that you have to think OO… Read more »

Charlie Hayes
12 years ago

“it generates better queries than the regular developer would write, and second – it generates bad queries if you use bad mappings” This is why you make sure your dev team isn’t made up of ‘regular developers’. Are ‘regular developers’ that write bad SQL able to make good mappings?

Sassan Aria
Sassan Aria
12 years ago

I am a big fan of ORM’s when they are used for the right project. The best candidate for ORM is a project for which there is a straight-forward data model with clear relationships that are expressed in terms of FK/PK and a 1-1 mapping between the fields in the beans/DTO and the table columns (in other words, the ORM Happy Path). Unfortunately, in my experience only a few of applications I’ve seen fall into this category (and they are typically simple apps, and relatively static in terms of data model requirements). The majority of the large scale applications (especially… Read more »

Richard Bucker
Richard Bucker
12 years ago

ORMs are good for simple CRUD so that you do not have to repeat various coding strategies… but it/they must be capable of supporting hand optimized SQL programming and should not have their own DSQL. (Domain Specific Query Language). That simply does not scale.

Bart Bakker
12 years ago

I posted a reaction on this post and more about treating frameworks: http://softwarecrafttalk.blogspot.com/2012/05/lifes-not-about-frameworks.html

Ian
Ian
10 years ago

I agree with all the comments so far and especially Sassan Aria’s comment. ORMs are one of those ‘good in theory’ ideas, but in the real world with real complexity and real database ugliness that occurs with any database that has been around a while, ORM’s fail at the last 10%. It’s a very well known problem.

marcello Dias
marcello Dias
10 years ago

Hi, I´ve never seen a regular developer, just good and bad ones. If you´re a bad one ,and don´t know SQL very much I suggest you read all Joe Celkos books. Joe Celko as far as i Know is one of the so called ORM haters. I´ve never even tried to use an ORM tool , just because nobody ever convinced me I really needed one. “You can code for every RDMS”,What about the SQL ANSI Syntax? If you change a field Type ,you don´t have to change code everywhwre,In 25 years I had to change the type of field… Read more »

Marcello Dias
Marcello Dias
10 years ago
Reply to  marcello Dias

Hi ,
When I said Joe Celko is an ORM Hater,I should have used commas,because Nobody is a hater of a technology,people like,dislike or ignore it,it was just to play with the articles name.
I myself desagree with Joe CElko in some points,I think he is too data minded in the case of not using surrogate keys.

Mic
Mic
10 years ago
Reply to  marcello Dias

Hey Marcello,

To answer your question, yes banks use ORMs, including in high-volume trading systems. That doesn’t inheritly make them good or bad, but it does mean that correctly implemented they can be stable.

ORMs are a tool, and in my opinion a valid tool. Under a lot of circumstances they can save a lot of dev hours in a system with many objects being persisted. That doesn’t mean they always need to be part of the stack either, but I think it’s worth having them in your proverbial toolkit in case.

Marcello Dias
Marcello Dias
10 years ago

Another thing,I don like to say an article is bad written or it is garbage. But saying that human resources cost more than machine time is a ridicolous thinking of someone who made software to himself or to use internally for just one company. How can you measure an hour of someone who coded for SAP,ORacle Business,JD Edwards,Microsoft DYnamics.software that runs in millions of machines in millions or at least hundreds of thousand servers? IF these guys have created slow software,would you really compare an hour of his work to the million dollars it would cost to buy more machines… Read more »

Toto
Toto
6 years ago

JPA was a good idea but completly srewed by a very poor implementation. Having to store relation on the two side objets is just a ridicoulous burden as the silly cascade management. In fact, solving the massive bugs is just consuming more time than the one saved by the ORM principle. Sad, very sad

Back to top button