ORM (Object Relational Mapping) in Java

The process of mapping Java classes with database tables, the properties of Java classes with the columns of a database table, and representing the objects of Java classes with the records of database tables having synchronization is called ORM or O-R mapping or Object Relational Mapping.

If the modifications done in objects are reflecting in the database table records and vice-versa then we can say objects and records of database tables are in synchronization.

Let us understand it through an example. Assume we have an Employee table in database software. Following is the database table structure,

create table EMPLOYEE (
   empid INT NOT NULL auto_increment,
   empname VARCHAR(20) default NULL,
   empsalary int default NULL,
   PRIMARY KEY (empid)

Now, let us create an Entity class representing the above table. The Java class that is mapped with database tables are called entity classes or persistence class. It is a Java bean class.

Employee.java (Entity class)

package com.know.hibernate;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name= "Employee")
public class Employee {

  private int eno; // primary key column

  // other columns
  private String ename;
  private float salary;

  // getter and setter methods
  public String getEno() {
    return eno;
  public void setEno(int eno) {
    this.eno = sno;
  public String getEname() {
    return ename;
  public void setEname(String ename) {
    this.ename = ename;
  public String getSalary() {
    return salary;
  public void setSalary(float salary) {
    this.salary = salary;

In one project there can be multiple classes and multiple database tables. In O-R mapping configurations, by using XML files or annotations (inside Java class) we can link/map specific Java classes with specific database tables. Now, due to configuration between Employee class (Entity class) and Employee table in the database, they will be in synchronization and their properties will be mapped with,

Entity class propertiesMapped Column

Note:- Entity class name and database table name may be different but it is recommended to take the same name. Similarly, Entity class properties and columns may have different names but generally, we take the same name. (Here, we have taken different names only for better understanding purpose).

Through the ORM framework, based on the primary key value each object will be mapped with one record. For every record (row in a table) one Entity class object can be created.

How synchronization is take place? If we modify salary for eno=101 to 20,000 in entity class then it will also reflect in the database table. Similarly, if we change ENAME for EMPID=102 in the database then it will also reflect in the entity class.

How Java Persistence Operations are done in ORM?

  • In O-R mapping persistence logic, we can do all persistence operations through objects without taking the support of SQL queries and JDBC code. Since working with objects is the same in all database software therefore we can say object-based persistence logic is database software independent persistence logic.
  • When we give objects to ORM software to perform persistence operations, the ORM software internally generates JDBC code and SQL queries dynamically as required for the underlying database software to complete the persistence operations. Unlike JDBC it doesn’t have boiler-plate code.

ORM framework will generate SQL queries dynamically at the run-time depending upon the database software. Approx 70% of SQL queries are similar in most of the RDBMS but the remaining are vary according to the database software. Database software information is given to the ORM framework through the configuration file.

There is no need of working with SQL queries, everything can be done through objects. From the developer’s point of view, no need to work with SQL queries, but to have the understanding they must have SQL knowledge.

How to Perform Different Operations using ORM?

How to insert the record? To insert the record, create one object, fill it up with data (initialize object properties), give it to the ORM framework to insert the record. Now, it is the ORM framework’s responsibility to collect the object data, internally generate database-dependent SQL queries to insert the record, convert object data to simple values, and finally insert the record.

How to get the record? To get and display the record, ask ORM framework, it will internally generate SQL queries to select the record based on the primary key value, get the record, push into the entity class object, and returns the object.

How to modify the record? To modify the record in a database table, modify the object data, now due to synchronization, records in the database table is also modified. ORM framework is internally generating the update SQL query.

How to delete the record? To delete the record, ask the ORM framework to delete the record, it will delete the record. The object will not be deleted, it can be used for another operation.

Different Operations in ORM

In O-R mapping we don’t use words insert, update, delete records instead we use save object, load object, update object, delete object. Following are their meanings,

OperationDescription (Meaning)
Save objectCollecting data from objects and writing/inserting that data to the database table as a record.
Load objectGetting the record from a database table and putting them into an Entity class object.
Update objectUpdating the record of a database table that is represented by an object of the Entity class.
Delete objectDeleting the record of a database table that is represented by an object of the Entity class.

Java ORM Frameworks

There are many object relational mapping software are in the market. Some of the top and popular ORM software are given below.

These were some top and popular ORM frameworks in Java. Some other ORM tools or software are,

  • JOOQ (Java Object Oriented Querying)
  • Java Persistence API (JPA)
  • JPOX
  • Kodo (Commercial)
  • Object Relational Bridge (Apache OJB)
  • ObjectDB
  • OJB (Object Java Beans)
  • OpenJPA
  • ORMLite
  • QuickDB ORM
  • Torque

Hibernate is the most widely used ORM framework in Java. It is a very stable framework and possesses many extra capabilities than other ORM tools available. Nowadays, developers started working with Spring Data JPA which provides an abstraction on ORM framework i.e. advance of ORM framework and less code compared to ORM tools. Therefore, for a better understanding of Spring Data JPA, knowledge of ORM tools (like Hibernate) is required.

We also have many ORM software or tools for non-Java. Examples:-

These were some top ORM software in non-Java languages, here you can get the list of object-relational mapping software.

Pros and Cons of Using ORM

Pros of using ORM

  1. Based on the configured database software, the ORM framework generates SQL queries itself at run-time. Therefore no need to write SQL queries.
  2. Many of the queries generated by the ORM framework perform better than the query generally we write.
  3. Moving from one database to another database becomes very easy because we have to write database independent ORM framework logic in our code.
  4. We can do everything with the help of Objects rather than database tables.
  5. It gives fast development of the application.
  6. We get a lot of advanced features like transactions, connection pooling, migrations, seeds, streams, and e.t.c.
  7. If you learn anyone ORM framework software then working with other ORM framework software becomes very easy.

Cons of using ORM

  1. If you are a master in writing efficient SQL queries then you can write better performant queries than ORM framework generated SQL queries.
  2. There is overhead involved in learning how to use any given ORM.
  3. In the initial learning phase of the ORM framework, the configuration can be a headache.
  4. From the developer’s point of view, we have to understand what is happening under the hood. Since ORMs can serve as a crutch to avoid understanding databases and SQL, it can make you a weaker developer in that portion of the stack. But we have to learn ORM because it gives fast development.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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