ORM Haters Don’t Get It

May 9, 2012

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.

If you find the content interesting, you can subscribe and get updates


 

22 Responses to “ORM Haters Don’t Get It”

  1. ORMs do stuff I don’t need them to do, like change tracking, identity management and lazy loading. In the end is about how much stuff you want/need a tool to do for you. And some Micro-ORMs out there are much faster.

  2. Full disclosure, I dislike ORMs. I like to use store serialized protocol buffers directly in a Blob column of the database, and use the extra columns for indexing purposes only.

    It is definitely not as powerful as an ORM, but I like the simplification. It is a type-safe approach however, and is resilient to protobuf schema changes.

    I’m curious to hear your thought about this style.

  3. One limitation I’ve found for ORMs like Hibernate is in reporting.

    For almost anything non trivial you’ll probably need to use native queries to obtain the exact results you’re looking for which kind of defeats the purpose of using an ORM. In my experience this happens a lot.

    Is JDBC or the likes an option?
    ===============================

    Not really! As you pointed out JDBC API is clumsy and you need to do a lot of DTO management.

    The middle road
    ===============

    Personally I’m quite found of MyBatis:

    - I can write my sql nicely in xml files (or annotations if I want); This sql I can easily test in my DB tool of choice.

    - If my tables names (or result aliases) match my domain model fields I get automatic mapping

    - Supports *toMany mappings

    - really fast learning curve. I’ve picked up MyBatis within a day.

    - build in caching or via plugins

    Downsides
    ———
    - MyBatis works well only if you like to design your DB first. Since data always outlives the application I firmly believe a good database design is crucial.

    - prototyping is slow; This can be alleviated up to some point by using MyBatis Generator

    Prototyping is slow due to

  4. Where’s the edit button? :(

  5. [...] ORM Haters Don’t Get It – [...]

  6. Good post!

    I also prefer MyBatis in some use cases. However, ORM is a good tool and helps in many situations to reduce effort.

    I would never ever use just SQL / JDBC or even Spring’s JdbcTemplate, if there are more than two or three queries to write…

  7. @Jacob Groundwater
    If you do this, you should not be using a relational database in the first place, and the whole ORM argument goes away. Some NoSQL store would suit you more.

  8. Full disclosure, I develop an ORM for a living.

    Handling data is important. And it’s quite complex. The bad rep of most ORMs usually comes from people who didn’t spend enough time learning how to use it.

    It is a complex domain and you should better know what you are doing. There isn’t a tool out there that can save you from negligence.

    There are two kinds of ORM tools. One is the complex, enterprisey, bloated with features, never leave the designer kind of ORM and the other one is a robust implementation of a mapper. I assume you are talking for the first one, it is also the kind of ORM I am working on.

    These tools usually come with a lot of hype and a lot of features, that people usually don’t understand. I cannot express how many customer solutions I’ve saw that did the wrong thing the wrong way. Developers need to wake up and realize it’s all about the DATA, and you can’t expect to plug in a 3rd party ORM to work out of the box.

    Actually I urge everybody to go ahead and implement it on their own. Use stored procedures, write your own queries, suffer and learn from the experience. Once you have faced and solved the issues of data management, see how an ORM can help, look at the best practices then and (maybe) only then, will you be able to take the most of such a tool.

    My advice: don’t overuse the ORM, use for the things you are comfortable with and things you know work.

    Sidenote: Something everybody does wrong is inheritance. The object/conceptual model allows for inheritance, however the relational/database one does not. What has happened is that every ORM out there has a different (and usually not optimal) implementation and as soon as you start using this shit hits the fan. Big time. Don’t use it.

  9. You mentioned the “you don’t need them, plain SQL or xDBC is fine” argument but didn’t go into it in detail.

    I’ve heard that argument before, and it’s exactly the same as the old “why do we need a higher-level programming language, it can never be as efficient as programming in assembly language” argument. Programming with JDBC is like programming in assembly language – it’s a lot of tedious work, it’s very platform-specific (despite the fact that JDBC tries to provide a common interface for all brands of databases) and it’s hard to maintain. And, exactly as you describe, you’ll find yourself building a simplistic mini-ORM someday.

  10. I don’t see what is wrong with using ORM for the trivial work and downgrade to raw SQL when you hit a performance bottleneck or your ORM just can’t handle a complex scenario. Even if you hit the problem because you are not good with the ORM then still it should not be a problem because you can always downgrade but you have the endless simple reads with just a filter or paging handled for you and this is a lot of work.

  11. I think that perhaps ORMs are a statically typed language thing, or perhaps a “Java-like” language thing.

    If you’re using a powerful functional language, which has lots of great primitives for processesing sets and maps, then an ORM is probably not all that useful as doing relational and functional programming on straight database output is trivial.

    There’s no Clojure ORM to my knowledge, and I’ve never heard of a Haskell ORM. These are tools for statically typed OOP languages like Java and C#.

    I’ve used DBIx::Class in Perl, and while its perhaps one of the best ORMs out there, I’m not even sure even its worth using. There’s a huge learning curve, and after you’ve mastered it you really don’t save yourself that much hassle over just writing libraries containing straight SQL. Its more portable than straight SQL, but that’s about all you get.

    So even in languages like Perl, which isn’t that exotic, ORMs probably aren’t really that smart a choice.

  12. I think I’ve started to dislike ORM for the two reasons you said: it’s very easy to misuse and uses a very complicated session management scheme (not to say other things).

    Someone said above that it’s all about data, and I can’t agree more! You have all these business problems you need to solve, and yet you have to stop everything and study very hard to understand all the intertwined concerns that affect how you are accessing your data.

    I’ve been using ORM in my latest projects, trying to keep the app database-agnostic and all that, but lately I’ve been thinking that maybe I would be better off choosing a database platform (PostgreSQL in my mind) and going all for it…

    I can see that many people find it complicated and complain about the wrong reasons, but maybe that’s all related to the problem that you said, in that it’s easy to misuse. I would add easy to misunderstand, but I guess it’s about the same thing.

    Cheers!
    Elias

  13. @Max Toro: Ever used Hibernate’s StatelessSession?
    @Jacob Groundwater: All the people, who don’t use Java, will probably don’t like you too much for this approach. Code comes and goes, but the data will stay and you store it in a language-specific way.
    @Elias: As Stilgar pointed out, that’s when an explicit data access layer pays off, because you should be able to transparently substitute a JPA DAO with a JDBC DAO any time.

    This is actually something I haven’t seen with any ORM yet: The ability to replace a JPQL or HQL query with a native one depending on the dialect, e.g. “for this type of database use this native query here in place of that generic query there”. Would be very helpful for DB-specific optimisation.

  14. You failed to list the single most important argument against ORM.

    Hierarchies, and Sets are fundamentally different things, and this creates an impedance mismatch that is fundamentally difficult to resolve when translating from one to the other.

    This isn’t to say that ORMs aren’t useful, only that one should be aware of this mismatch when trying to translate data and functions from one domain to another.

  15. I don’t like ORMs, but I use them, when I have good reasons to use them, because they can do a fantastic job.
    BUT, they also can be chosen for bad reasons. For instance, one of my customer wanted to use Hibernate because he didn’t want to manage the DB. Most of the developers were Java developers with no knowledge of DB.
    So, they decide to ignore the DB, and use Hibernate to hide it.
    And guess what ? The default generated schema is a mess.
    Of course, the issue here is the people, not the ORM.
    But ORM can give the impression to known nothing about DB and be able to handle it.
    There need to be specific development rules when an ORM is used : DB schema review, DB queries review …
    With a team of experienced people, ORM usage is a pleasure. With a team of beginners, it can be very painfull.

  16. “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”

    Where is the problem ? The problem, in this case, is by misused , not an ORM issue.

    For me, a big problem using ORM is they generate enhanced classes and serialization can be a problem

  17. Very well said, I would prefer to have a library with little bit of bugs instead of letting developers re-invent the same things on their own and end up creating a disaster.

    Many apps, which do not use ORM like frameworks struggle in performance since not everyone know how to write best performance queries.

    Also there is a high chance that ORM tool would generate a better or equivalent query to average sql coder.

    I have seen many old projects which did not use ORMs tend to create their own at the later stage and then have nightmare moving away from them.

  18. Well Bozho I’m going to have to disagree. You state that if I choose to write an ORM, it will “suck”. Wouldn’t this also hold true for the authors of the many popular ORMs already in use? Or do these developers have some special knowledge of the dark arts that allows their code to be magically good and mine to “suck”?

    It’s true many custom built ORMs do suck but I make the argument that this is due to a lack of experience on the developer’s behalf and that no ORM is going to compensate for someone that doesn’t have the skills required to build a decent database with decent SQL. If you can’t trust your developers to write a proper and simple ORM then I suggest you get better developers rather than installing an ORM.

  19. First, writing a good ORM is tricky. I don’t think any developer out there can do it right. I’m not even sure I would be able to do it right if I hadn’t seen many other ORMs.

    Second, it’s about focus – your focus is to make a business application, not an ORM. The ORM is just a building block, and you can’t be working full-time on the ORM. That was not the case for existing ORMs.

    And third, existing ORMs have been out there for ages – major bugs and problems have been addressed and fixed. Your new ORM is likely to suffer from many of these bugs, until it becomes mature.

  20. [...] пост на Божо за ORM технологиите – http://techblog.bozho.net/?p=886 | август 11th, 2013 | Posted in Uncategorized [...]

  21. I am entirely for using ORMs. I just don’t believe it’s reasonable to use them 100% of the time.
    For me the biggest disadvantage of an ORM is that
    * it produces hard to read SQL
    * it hides major features of the database like creating temporary tables which are awesome for improving performance of complex queries (hundreds of lines of SQL)

    These points make difficult the life of a person who is not a developer but is tasked with writing/optimizing/debugging queries based on some complex business requirements. Being able to execute hand-written native SQL queries shines in this situation. And no – createNativeQuery() is not the solution because it only executes a single SQL query.
    That being said, ORMs are great for everything else.

    Finally I am curious about your main argument. It seems like a very wild guess that everyone is eventually going to implement his own silly ORM. I’ve seen many project where people were successfully using plain xDBC and never looked to implement/use any kind of ORM.

  22. If yоս will do sߋmething with thеѕe ѕuggestіons ƴojr сelebratiοn will Ьe funny
    enouǥɦ. On July 31st, Funny Ƥeople will hit movie theatreѕ.
    The dіfference betweesn the tеenagеr and the pаrent іs that the tеeոagеr still has the faults thе рarent outɡгew.
    Ethan: Theո don’t tɑkе me tօ a waffle houѕe!
    As he woke up, he realized that his cap weren’t whеre he lеft thеm.
    They cаn also Ьefunny, crazy, and hilarious.
    Screen printing method of prіոt. Ӏf you arre protesting naƙed, where
    arе you ɡoing tto put youг prokf of age?
    Firѕt of all, you should refer some poetry bօoks oof good authoгs tҺat will рrove to be a great helping hand in writinց poems.

    Alsο exprеssing your emotion and heartilƴ feelings will
    giѵe a soft touchh to the funny ƿoеms. Conclusіon: In tɦе end,
    Ӏ must conclude that selling pictures or videos online is
    also a very profitable way to еarn wаy. Anyway, my non-nun music tеacher leet us ρlay this game at the end of music
    claѕs, wheгe someone would go stаnd in the corner anԁ cpѵer ɦis eyеs while someone else went tօ the back of
    the roοm ɑոd saіd the other persoո’s name. If you are faіr waiting fօr problem to
    get еlimіnated itself, expect me it leaves never doеs. Βy аdamleaf :
    A hoow to tutorial about funny tees, fuոny t shirt, funny tshirts,
    Shoppіng with step bу step guide from aɗamleaf.
    Ed Haгkeո: Dammіt. Anԁ tҺat tҺoughttook aԝay all my ambition too.
    Mothers Day iѕ cominǥ up right аrouund thee corneг.
    Humоr iss a uniգue human trait.

    Any of tҺese words will genеrally do the trick:
    butt, poop, fart, diaper, underwear, stinky feet, armpit, silly,
    ѡeird, cгazy. They are aimeԁ at everything from traіns, maгrіage, and rаnching, to encyclopedias аnd alcohоl.

    Jacҡ Byrnes: Ƥam’s miԀdle name. Theгe are so mɑny funny animɑl
    videο iո eѵerʏ socіal vіdeo webѕite, if you feel a
    littlе lopnelү of your daіly lifе, іf yoս
    aгe thinking that there is so much sad emotіߋn during your day.

    Оpting to write on these poems yоս may fiոd it easy but ոot if
    yyou have to write oon funy poems. Thіs is the day
    whhen wе forget abߋսt our daily stress of life aոd we want to hɑve fun with our friends and family.
    They may bee a bit similаr to the Ϲhristmas
    sayingѕ, because theѕe two holidayѕ share thе same tгaditions, but yoս can
    enjoy rеading them separаtely. The gеek t shirts ѡill ƅe a pеrfect Ƅuy for ѕomeone
    wwho loѵes theіr computers, ɡadgets and anything expensive that
    is an electronіc.

    Resolutioոs caan be pretty serious, especially New Yеars Resolutions.
    Spеесhеs aгe ոot just boring pieces of informаtion
    that ոeed to be fеd to an auԀience. This game haѕ yoս giνе each giest oո
    аrrival a small token. You ѕaу hhe wears a beard, has
    no discernible source of inϲome and flueѕ to
    citіes all oνer the ѡorld undsr cover of dɑrkneѕs?
    Or ɑre you one of the few lucky ones who finallly found sօmeߋne worthy oof everʏ love iin
    the world? Well, at least we were ɡoing somewheгe.

    He is Noe Buԁdy.” “Oh my, I am shivering now! He lіkes рickles,
    apples, neгds, carrots, gսmmy cokes, ɑnd aboսt ԝhatever he can get his teеtҺ on.

    Dear restroom, yoս arеn’t just a bathroom. Noe Van hаs been іnjured and іѕ in the
    hospital. Just Be Yourѕelf You know it’s so obvious wheո someone
    is “trying” to bbe funny by doing oг sayinɡ something ߋut of their character?
    People love being told how wonderful thеy are and it
    makes the daү а ԝhole lot ƅrigɦter for eѵeryone.
    Tell all they are not to utter tҺiѕ woгd at
    all duгinɡ the Сhristmas occasioո yоu celebrate. 1) Life is like a beаutiful
    sоng–only the lʏгics ɑre all screwed up. The сousin hopped into heг carr and
    went drivinǥ down thе street after my Gгandmother. Tο make yoսr work pгаiseworthy
    just do it sіncerely aloոg with somе inոovative ideas to get tɦe
    positive result. Αre you lookіnɡ for funny Chrіstmas quotes and sayings?

    Ron Burgundy: What are you doing? To ɦis dіsmay, he saw a
    lion advaոcing towrds thе tree. Doorman: Yоu old, she prеgnant.

    FunnЬy Quotes about thе New Yeear You can peгsߋոalize yߋur New Year wishes by adding tҺese quotes.
    Youu try your hardest to raise your tеenagers wіth patience, honesty and good manneгs, but tҺеy stіll ennd up being liike you.
    Eҳcept for heгpes. Nߋ mɑttеr what the genre is,
    mߋvieѕ lеt us orget аnything and eѵerything that’ѕ going wrong
    in our livеs. Look, I don’t speak Spaոish.

    Roҳanne Ritchi: Ρleaѕe tawlk sloѡer. The generɑl reaction foг jokes could be
    laughter if ոoot a ѕlight twitch of smile within the deаl with of
    onlookeгs or listeneгs. Some of tɦe news webѕіtеs provide latest updated funny news on a
    daily basis. An additional funny law in Georgia protects ʏou from privatе lotterіes.
    This is how great comedians can elicit lаughter out
    off old jokes ɑ

Leave a Reply