What is an ORM (Object Relational Mapping) ?


Many of you heard a term ORM or Object Relational Mapping and thought that What is an ORM (Object Relational Mapping) ? ORM is a short form of Object Relational Mapping. ORM is a concept which encapsulate relational database schema and provide a object oriented interface to developers. ORM framework as are developed using an object oriented programming language like PHP, Java etc. ORM allow developers to query and manipulate the data from a relational database using an object oriented paradigm. ORM wrap around the relational database system (RDBMS) and create a virtual object oriented database that is used within and from the programming language.
Basically ORM define a mapping between your classes and database tables or class members and table columns and the ORM manage the communication between your codes and underlying database. ORM framework wrap relation database tables into the classes of programming language, so that instead of writing SQL queries to manipulate database, we can use methods and objects.


Now question is that Why i use ORM frameworks? What are advantages of ORM ? So, here is your answer

Advantages of using ORM frameworks

  1. ORM framework protect you application against the SQL injection attacks. Hence the ORM frameworks filter and sanitize all data. So, you don’t need to worry about this.
  2. Since ORM handle all database related things like writing SQL statements, data type conversions etc. SO, it become easy in future if you want to change the database server of your application. Like your application currently use MySql and you need to use PostgreSQL as database. It become very easy and need very less efforts if you use an ORM. By the way in my six year of experience i seen this case only once.
  3. If you have more then 10 tables and more then 3 developers are working on an application. Then it is helpful to use ORM framework. Because ORM framework create a consistent code style for your application. So, it becomes easy to work and probability of meshing up become less.
  4. There are lots of stuffs are done automatically for you by ORM frameworks. For example database handling, table relations & joins, filtering data, I18N etc.
  5. It make the maintenance work easy for you. Because ORM framework follow DRY practice. So, you have to write your data models only at one place. So, it become easy to maintain and update codes.
  6. A good ORM take care of synchronisation of the data types between the programming language (Ex. PHP, Java) and the SQL database. So, it also handle the data type conversion related things for you.
  7. ORM supports to ACID properties and transactions (commit and rollback).

If i am not wrong now you are thinking that Does there are some drawbacks of using ORM framework. So, here is your answer

Disadvantages of using ORM frameworks

  1. You have to learn the ORM framework first. There is a learning curve in understanding and using ORM framework.
  2. ORM makes working easier. But unfortunately developers skip learning SQL and database related things.
  3. Performance is fine for normal queries. But but you can always do better with your own SQL queries.
  4. Some time ORM fails to compete against native SQL queries incase of complex queries.

6 comments on “What is an ORM (Object Relational Mapping) ?”

  • If you have more then 10 tables and more then 3 developers are working on an application. Then it is helpful to use ORM framework.

    Hmmm… A little wrong. If you have 100 tables with 5 000 000 records and gigs in some of them, foreign key relations in tables and on top of that need the system to work RUN AWAY FROM ORM. Applications just lag, I have seen java android app to make phone hot on 10000 records using orm and then work 100 times faster and no heat with 2000000 records, 5000 coming each second for testing when skipping orm in favour of sql. Php project to give realtime reports and refresh data on large dbs. Forget it with orm. Doctrine makes apis to need something like 300mb and respond in 1-2s where raw sql and less php oo overhead will work with 4-6mb ram usage and 0.01s needed to complete.

    You don’t need to change db each day and this usually means code rewrite. Many of the “advantages” of orm cited are fake if people think from real world cases point of view.

    Orm is good only for small, very little loads, standard crud(no analysis and complex reports) sites/apps or prototypes. Bloated code they force you to write makes dev speed go down on more compelxity- something like using all bloat 5 men to write for a year what in raw php olus sql will be done by 1 in a month(and this plus the performance and more dificult support problems of bloated code).

  • For this reason, many programmers find themselves more at home with an object-SQL mapping system, even though most object-oriented databases are able to process SQL queries to a limited extent.

  • We are a group of volunteers and opening a new scheme in our community.

    Your website offered us with valuable information to work on. You’ve done an impressive job and our whole community
    will be grateful to you.

  • ORMs are an anti-pattern! No matter what size the project or the database, you’re better off not using an ORM.
    “Object-Relational impedance” is an imaginary problem. You really need to evaluate, what does an ORM give the project vs. simple connection pool and transaction managing objects. Most large projects incorporate an ORM purely because it is seen as the professional way to do things, and that’s why, as a programmer, you’ll have to learn them. But as far as real value, ORMs only serve to bloat and add complexity to systems. Your system SHOULD contain some database management object with an interface like this:

    public PreparedStatement prepareStatement(String statement);
    public PreparedStatement prepareInsertStatement(String statement);
    public ResultSet getResult(PreparedStatement preparedStatement);
    public void doUpdate(PreparedStatement preparedStatement);
    public void doDelete(PreparedStatement preparedStatement);
    public int doInsert(PreparedStatement preparedStatement) throws SQLException;
    public List doSelect(PreparedStatement preparedStatement);

    and this pattern will give you virtually every advantage of an ORM. The fact that some ORMs actually incorporate their own SQL like languages like HQL and JPAQL should be a major clue to their complete redundancy. Also notice that most of them have an “escape hatch” that lets you bypass them, e.g. the “named native query” of Hibernate, and simply use SQL. You’ll find yourself resorting to this a lot as you code with an ORM and it should make you wonder what use it had in the first place.

Leave a Reply

Your email address will not be published. Required fields are marked *