Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should you use an ORM? [closed]

Tags:

database

orm

People also ask

When should you not use an ORM?

Whether or not you should use ORM isn't about other people's values, or even your own. It's about choosing the right technique for your application based on its technical requirements. Use ORM or don't based not on personal values but on what your app needs more: control over data access, or less code to maintain.

What is an ORM and why would you use it?

An object-relational mapper (ORM) is a code library that automates the transfer of data stored in relational database tables into objects that are more commonly used in application code.

Is it better to use ORM?

ORM and SQL are two tools available that web developers can use in database management. When comparing them, SQL has a higher hands-on management than ORM. Because ORM has a higher level of abstraction and more complexity than SQL, less hands-on management is required; this makes data management more efficient.

Why do we need ORM tools like Hibernate?

ORM tools like hibernate provide following benefits : Improved performance : Lazy loading, Sophisticated caching, Eager loading. Improved productivity : High-level object-oriented API, Less Java code to write, No SQL to write.


The most important reason to use an ORM is so that you can have a rich, object oriented business model and still be able to store it and write effective queries quickly against a relational database. From my viewpoint, I don't see any real advantages that a good ORM gives you when compared with other generated DAL's other than the advanced types of queries you can write.

One type of query I am thinking of is a polymorphic query. A simple ORM query might select all shapes in your database. You get a collection of shapes back. But each instance is a square, circle or rectangle according to its discriminator.

Another type of query would be one that eagerly fetches an object and one or more related objects or collections in a single database call. e.g. Each shape object is returned with its vertex and side collections populated.

I'm sorry to disagree with so many others here, but I don't think that code generation is a good enough reason by itself to go with an ORM. You can write or find many good DAL templates for code generators that do not have the conceptual or performance overhead that ORM's do.

Or, if you think that you don't need to know how to write good SQL to use an ORM, again, I disagree. It might be true that from the perspective of writing single queries, relying on an ORM is easier. But, with ORM's it is far too easy to create poor performing routines when developers don't understand how their queries work with the ORM and the SQL they translate into.

Having a data layer that works against multiple databases can be a benefit. It's not one that I have had to rely on that often though.

In the end, I have to reiterate that in my experience, if you are not using the more advanced query features of your ORM, there are other options that solve the remaining problems with less learning and fewer CPU cycles.

Oh yeah, some developers do find working with ORM's to be fun so ORM's are also good from the keep-your-developers-happy perspective. =)


Speeding development. For example, eliminating repetitive code like mapping query result fields to object members and vice-versa.


Making data access more abstract and portable. ORM implementation classes know how to write vendor-specific SQL, so you don't have to.


Supporting OO encapsulation of business rules in your data access layer. You can write (and debug) business rules in your application language of preference, instead of clunky trigger and stored procedure languages.


Generating boilerplate code for basic CRUD operations. Some ORM frameworks can inspect database metadata directly, read metadata mapping files, or use declarative class properties.