Hibernate vs JDBC

Hibernate vs JDBC | Here we will discuss the difference between JDBC and Hibernate. What are the limitations of JDBC and why we should go for Hibernate framework? We will see the table for JDBC vs Hibernate.

Java Database Connectivity (JDBC) is an application programming interface (API) for the Java programming language, which defines how a client may access a database. It is a Java-based data access technology used for Java to database connectivity and it is part of the Java Standard Edition platform and given by Oracle corporation. It provides methods to query and update data in a database and is oriented toward relational databases.

Hibernate is an open-source, non-invasive, light-weight Java ORM(Object-relational mapping) framework to develop objects based, database software independent persistence logic. It is an implementation of JPA(Java Persistence API).

Difference between JDBC and Hibernate

Now let see comparison between Hibernate vs JDBC in detail.

1) Hibernate code is database software independent but the JDBC code is database software dependent.

2) Hibernate logic is portable but JDBC persistence logic is not portable across the multiple database software. While working with JDBC code, moving from one database software to another is complex.

3) JDBC code is having a boilerplate code problem. The code that repeats across the multiple parts of the project either with no changes or with minor changes is called boilerplate code.

4) Most of the JDBC API methods throws SQLException. For all problems, we get SQLException, which doesn’t give more idea about the problem, and solving those problems becomes complex. SQLException raised by JDBC API method is checked exception therefore it forces the developers to catch and handle the exception. We generally catch & handle the exception using try/catch blocks which suppress the raised SQLException in the current method definition. To send that exception from one method to another, the programmer must write code for exception propagation.

Exceptions raised by Hibernate framework are unchecked exceptions, and it doesn’t force developers to catch and handle the exception. These exceptions can be passed to the caller method without writing explicit code.

5) In JDBC, the SQL query can’t carry Java objects as directly input values. Through input values are in the form of objects, we need to convert the data of the object to text values/simple values in order to send it to the database software along with SQL query as input values. Therefore while working with JDBC, we are object-oriented programmers, but we don’t have the freedom to send objects directly as a query parameter value. But in Hibernate, we work with Entity class and can enjoy object-oriented programming features.

6) JDBC Resultset objects are not Serializable, but Hibernate entity class can be Serializable.

We can’t write normal object data to the files or over the network. Only serializable object data can be written to files and can be sent over the network. If the Java class is implementing java.io.Serializable(I) then its objects are called Serializable objects otherwise objects are called non-serializable objects (normal objects).

In JDBC, the SQL select query execution gives the JDBC ResultSet object having a bunch of records. But this ResultSet object is not a Serializable object to send over the network. To send these ResultSet objects over the network generally, we copy the ResultSet object records to Java collections like List, Map, Set, and e.t.c. By default all Java collections classes are serializable. This process gives an extra burden to the programmer.

But Hibernate uses Entity class and we can make them Serializable (by implementing it from java.io.Serializable). Therefore, we can send them over the network.

7) There is no support for a built-in cache in JDBC but Hibernate provides the built-in caching feature.

There is no support for built-in cache to reduce network round trips between Java application and database software across the multiple executions of the same SQL query with the same inputs. By default, JDBC does not support caching and we need to implement the cache manually which is a complex process and increases the burden to the programmer.

8) JDBC doesn’t provide built-in features for versioning of the record (i.e. keeping track of how many times the record has been modified). In Hibernate framework, versioning is a built-in feature, and to use it just enable them.

Some examples where versioning is using:- There are limitations on the number of free ATM transaction (like 4 times in a month), once it is over bank will charge for each transaction. In banks, after a certain number of times if you change the phone number then they will charge.

9) In JDBC, There is no support for time stamping as a built-in feature (i.e. keeping track of when the record is inserted and lastly updated). Hibernate gives a built-in time stamping feature. An example of time stamping is bank transactions. Bank keeps a record of each transaction with time and location.


It is Java technology.It is a Java framework.
It is an implementation of JDBC API.It is an implementation of JPA(Java Persistence API).
JDBC doesn’t provide built-in features, we need to write everything manually.Hibernate provide built-in features for caching, versioning, time stamping, lazy loading and e.t.c.
JDBC code is database dependent.Hibernate code is database software independent.
It is not portable across multiple database software.It is portable across multiple database software.
JDBC ResultSet is non-Serializable object.Entity class in Hibernate can be Serializable.
Exceptions raised by JDBC methods are checked exception.Exceptions raised by Hibernate are unchecked exception.

JDBC vs Hibernate – Database Dependency

The JDBC code is database software-dependent code but Hibernate is database software independent code.

The JDBC code uses SQL queries in the persistence logic development. In Oracle, MySQL, PostgreSQL databases, approx 70% SQL queries are same in all database but remaining 30% SQL queries (like data types, aggregate functions like count(), max(), min() e.t.c.) are different in different database software. Since the SQL queries are database software-dependent queries. Therefore the JDBC persistence logic becomes database software-dependent persistence logic. Hibernate code internally generates JDBC codes with SQL queries based on the configured database software. While working with Hibernate, moving from one database to another database becomes very easy but JDBC doesn’t give this freedom, we need to write code once again from the scratch.

Due to the above reason, the JDBC persistence logic is not portable across the multiple database software, but Hibernate persistence logic is portable.

Why did we need to change the database software? Generally, during project development and testing time, the software companies use most of the free services, software, and tools to minimize the development cost. But in the production environment, to improve security, performance, and e.t.c. commercial tools and services and used. Example:- For large/medium scale projects, at the development phase MySQL database used (which is free), but in the production environment, Oracle database is used (which is commercial).

Another example:- You started a startup. In the initial days, due to financial and other reasons, you used free tools like MySQL database software. After sometimes the startup becomes successful and lots of customers joined with you. To expand the business, to improve security and performance now you want to use commercial software like Oracle database. If the Java persistence code was developed using JDBC (which is database software-dependent logic) then you have to write code once again from the scratch level using Oracle database software, which can take lots of time.

JDBC vs Hibernate – Boiler Plate Code Problem

The code that repeats across the multiple parts of the project either with no changes or with minor changes is called boilerplate code. JDBC code is having a boilerplate code problem.

Example of inserting record using JDBC,
a) Load JDBC driver class to register JDBC driver.
b) Establish the connection with the database.
c) Create JDBC Statement object.
d) Send and execute insert SQL query in database software.
e) Gather SQL query results from database software and process them.
f) Close JDBC connection with database software.

Example of updating record using JDBC,
a) Load JDBC driver class to register JDBC driver.
b) Establish the connection with the database.
c) Create JDBC Statement object.
d) Send and update SQL query.
e) Gather results and process results.
f) Close JDBC connection with database software.

In these two examples a, b, c, and e are common logics but d, and e are application-specific logic. All JDBC program contains common a, b, c, and e logics. This common logic is called boilerplate code. Hibernate doesn’t contain these boilerplate codes.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do 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 *