Object-Relational Mapping (ORM) Tutorial

What is ORM

ORM stands for Object-Relational Mapping (ORM) is a programming technique for converting data between relational databases and object oriented programming languages such as Java.

Using an Object Relational Mapping library improves the application development process and runtime handling in many aspects. To handle the communication between the Object Domain model (Classes/Objects) and Relational Model (Table/Records), we would be wasting a lot of time in writing boiler plate template code. To ensure a seamless communication with robust implementation and good performance, it is better to embrace an already available ORM like Hibernate.

ORM is a programming method to map the objects in java with the relational entities in the database. In this,entities/classes refers to table in database,instance of classes refers to rows and attributes of instances of classes refers to column of table in database. This provides solutions to the problems arise while developing persistence applications using traditional JDBC method. This also reduces the code that needs to be written.

Why Object Relational Mapping (ORM)?

When we work with an object-oriented systems, there's a mismatch between the object model and the relational database. RDBMSs represent data in a tabular format whereas object-oriented languages, such as Java represent it as an interconnected graph of objects.

Loading and storing objects in a relational database exposes us to the following mismatch problems.
  • Granularity Sometimes you will have an object model which has more classes than the number of corresponding tables in the database.
  • Inheritance RDBMSs do not define anything similar to Inheritance which is a natural paradigm in object-oriented programming languages.
  • Identity A RDBMS defines exactly one notion of 'sameness': the primary key. Java, however, defines both object identity (a==b) and object equality (a.equals(b)).
  • Associations Object-oriented languages represent associations using object references where as am RDBMS represents an association as a foreign key column.
  • Navigation The ways you access objects in Java and in a RDBMS are fundamentally different.
The Object-Relational Mapping (ORM) is the solution to handle all the above impedance mismatches.

Advantages of ORM

An ORM system has following advantages over plain JDBC :
  • Improved productivity
    • High-level object-oriented API
    • Less Java code to write
    • No SQL to write
  • Improved performance
    • Sophisticated caching
    • Lazy loading
    • Eager loading
  • Improved maintainability
    • A lot less code to write
  • Improved portability
    • ORM framework generates database-specific SQL for you

Comparison of ORM with JDBC

Compared to traditional techniques of exchange between an object-oriented language and a relational database, ORM often reduces the amount of code that needs to be written.

Disadvantages of ORM tools generally stem from the high level of abstraction obscuring what is actually happening in the implementation code. Also, heavy reliance on ORM software has been cited as a major factor in producing poorly designed databases.

NEXT READ Hibernate Intoduction
Object-Relational Mapping (ORM) Tutorial

Follow us on social media to get latest tutorials, tips and tricks on java.

Please share us on social media if you like the tutorial.
    Blogger Comment
    Facebook Comment