Consider the Employees table having the following records:

An Object-Relational Mapping (ORM) provides a framework that allows applications to query and manipulate data from a database using an object paradigm. The framework is implemented in many languages that encapsulate the code needed to manipulate the data. That way, you don’t need to know SQL specifics but can access the data using an object that hides that every database has variations in query language.

.

Following is an example that retrieves ID, Name and Salary columns for customers available in an Employees table:

SQL> SELECT ID, NAME, SALARY FROM Employees where ID = 1 ;

Programmatically, you have to create the following code:

String selectSQL = "SELECT ID, NAME FROM Employees WHERE ID = 1" ; Statement = dbConnection.createStatement() ; ResultSet rs = Statement.executeQuery(selectSQL ) ; while (rs.next()) { String userid = rs.getString("ID") ; String username = rs.getString("NAME") ; }

With an ORM, the code changes to:

Employee_list = EmployeeTable.query(ID=1) ;

As you can see, all the complexity is handled automatically by the ORM. There are many additional benefits when using ORM ; for example, it:

Saves time since data model is in one place.

Is easier to update, maintain and reuse the code.

Is very flexible because you code in your preferred programming language.

Of course, you have to spend time learning ORM to enjoy these benefits. There are a lot of ORMs around like Hibernate (Java), Propel or Doctrine (PHP), Django or SQLAlchemy (Python) etc.

In this post, we focus on Hibernate. Hibernate ORM (Hibernate) is an object-relational mapping framework for the Java language, which provides a framework for mapping an object-oriented domain model to a traditional relational database. Hibernate solves object-relational impedance mismatch problems by replacing direct persistence-related database accesses with high-level object handling functions. Hibernate is free software that is distributed under the GNU Lesser General Public License 2.1. Features include mapping from Java classes to database tables (and from Java data types to SQL data types), provides data query and retrieval facilities. It generates SQL calls and relieves the developer from manual result set handling and object conversion. Applications using Hibernate are portable to support SQL databases with little performance overhead.

Hibernate sits between traditional Java objects and the database server handling all the work required to persist those objects based on the appropriate O/R mechanisms and patterns.