JDBC Vs Hibernate – A Comparison / Difference between JDBC and Hibernate

JDBC is an acronym for Java DataBase Connectivity and is a technology for interaction of java application and its objects with a database. Hibernate on the other hand is a java based framework which also facilitates the interaction of application objects with a database but in a completely different approach.

Below are listed some differences between the two. The points do not intend to indicate which is better, they just compare the various aspects of the method of operation of both. The headers before each row indicate the area of comparison.

JDBC is a technology for persistence of data. Hibernate is a framework for persistence of objects.
Query Language
JDBC uses SQL (Structured Query Language) to interact with the database. Hibernate uses HQL primarily uses (Hibernate Query Language) and also SQL (Structured Query Language) to interact with the database.
Database Dependence
JDBC uses SQL queries for database interaction which vary with the type of database. Hence, when the underlying database changes, queries have to be updated. Hibernate uses HQL which are independent of database. Hence, changing application database requires no change in application code.
JDBC provides no caching support. It has to be separately implemented and integrated with the application. Hibernate provides two levels of caching. Separate implementation is not required.
Connection pooling
JDBC by itself does not provide any connection pooling facility. It has to be implemented by the developer using java or third party libraries such as DBCP from Apache. With Hibernate you can use connection pooling merely by adding a dependency in your application build file and configuring it in hibernate configuration file. Example is c3p0, which has a hibernate package.
Multiple Table data fetch
If you have nested objects (whose data is distributed over multiple tables), then SQL queries to fetch the data can become too complex. For example, suppose you have employee’s official details in one table and his personal details in some other table then the query to fetch complete employee data will be quite long and cumbersome. Hibernate’s One-To-One, One-To-Many and Many-To-Many relations manage this very well and fetching top level entity will automatically fetch the data from all related tables automatically.
Object-Database table Relationship
In order to convert database record into an object, explicit code needs to be written where each column data will be mapped to its respective java class field. Hibernate automatically does this via configuration file or annotations. Moreover, column names are automatically mapped with the respective entity fields if they share the same name.
Primary Key Generation
With JDBC we cannot auto generate primary key value for a record, we need to explicitly set it before saving. Hibernate provides automatic primary key generation so that when we save a record, primary key value for that record is automatically generated.
An object may contain another object such as a Test Script and a list of its test steps. When you save (or delete) a test script, you will obviously want the test steps to be saved (or deleted) as well. This is called Cascading. With JDBC, this has to be manually handled by the developer. Save the test script, then set the id of this test script in each of the test steps as a foreign key; delete the test script, then delete all test steps related to the given test script id and so on. Hibernate manages all this stuff by itself. You just need to apply appropriate Cascade and CascadeType either in the form of annotations or in configuration file.
Automatic Database Creation
You cannot create a non-existent database or table if you are using JDBC provided you don’t execute a query to do that. With Hibernate you can create database or table at application startup by using appropriate value of configuration property hbm2ddl.auto.
Query Statistics
With JDBC, it is not possible to figure out the time taken by a query unless you manually calculate the time or set logging at database level. Hibernate can easily generate the statistics for query execution in an application. It can also tell the number of database connections, statements utilized and the time taken for these operations, the cache usage and number of flushes. All required is the configuration hibernate.generate_statistics to be set.
Row Versioning
Versioning of rows is not possible using JDBC. Although this can also be managed explicitly by incrementing a particular column value after every update. But again its a manual effort. Hibernate manages versioning automatically provided that you have configured a field to be used as a version by annotating it with @Version or by setting a field as versioned using in XML configuration.

Hope the points made the comparison clear and worth reading. If yes, then show the agreement by giving your comments and don’t forget to share the post. coDippa !!!