Monday, September 2, 2024

10 Difference between Hibernate and JDBC in Java

JDBC is the core technology to access database from Java application but it require lot of boiler plate coding. Hibernate on the other hand an ORM (Object Relational Mapping) framework, which automatically map Java object to tables and records in database. You don't need to execute query to populate objects, Hibernate will do that for you. It also provides several benefit as compared to JDBC e.g. performance improvement via Proxy, lazy loading of associations and Caching, that's why both enterprise Java application and web application uses Hibernate to implement the DAO layer of application.

From interview point of view, difference between Hibernate and JDBC or advantage of using Hibernate over JDBC is one of the frequently asked question and if you mention Hibernate in your resume, you must understand why you use it over a traditional technology like JDBC. 

In this article, I'll show 10 basic difference between Hibernate and JDBC in Java.


Difference between Hibernate vs JDBC

As I told fundamental difference between Hibernate and JDBC is that former is an ORM framework while later is an API which allows Java programs to access database.

In case of JDBC, you have full control of your data access code e.g. what SQL your code should execute, but in case of Hibernate, it's the framework which executes SQL on your behalf. 

Though you can see what SQL queries Hibernate is executing by setting show_sql property to true. Anyway, here are some of the key differences between Hibernate and JDBC technology:


1. Database Independence

One of the great advantage of using Hibernate is that it makes your DAO layer database independent i.e. your code will work for all database vendors e.g. Oracle 11g, MySQL, SQL SERVER, PostgreSQL, IBM DB2 etc. 

On the other hand since JDBC executes SQL queries and if you are using database specific features than your code will become database dependent. 

This is why when you migrate from Oracle to SQL SERVER, you need to make lot of changes in your SQL queries and DAO classes. 


2. Object Oriented vs SQL

Another key difference between Hibernate and JDBC is that As Hibernate is set of Objects, you don't need to learn SQL language. 

You can treat TABLE as a Object. Only Java knowledge is need. 

In case of JDBC you need to learn SQL. 


3. SQL query Tuning

You don't need Query tuning in case of Hibernate. If you use Criteria Quires in Hibernate then hibernate automatically tuned your query and return best result with performance. In case of JDBC you need to tune your queries.


4. Caching

You will get benefit of Cache. Hibernate supports two level of cache, First level and 2nd level. So you can store your data into Cache for better performance. 

In case of JDBC you need to implement your java cache. 


5. Statistics

Hibernate supports Query cache and It will provide the statistics about your query and database status. 

JDBC don't provide any statistics. 


6. Development time

One of the most important difference between Hibernate and JDBC and probably the single most reason to choose Hibernate over JDBC is Development time. In general, development is fast in case of Hibernate because you don't need to write SQL queries. Add some out-of-box caching and lazy loading onto that. 


7. Connection Pool

No need to create any connection pool in case of Hibernate. You can use connection pool maintained by hibernate. In case of JDBC you need to write your own connection pool or you can use a third party library like Apache DB connection pool of c34j.


8. Relationship Visibility

Another useful difference between Hibernate and JDBC is that in case of Hibernate, you have a mapping file between object and table and in that xml file you can see all the relations between tables in case of Hibernate. Easy readability. 


9. Lazy Loading

You can load your objects on start up using lazy=false in case of Hibernate. 

JDBC Don't have such support. 


10. Versioning

Another interesting difference between Hibernate and JDBC API is that Hibernate supports automatic versioning of rows but JDBC doesn't have support for that.

Here is also a nice diagram which provides point-based difference between JDBC and Hibernate for quick revision:



That's all about difference between Hibernate and JDBC in Java. While both Hibernate and JDBC are great tool to access database from Java application, its clear that Hibernate is more feature rich but JDBC also have its own advantage like you can use plain SQL to interact with database. 

You can also use Stored procedure to access data while using JDBC. I have seen this pattern in multiple places and it work really well .

You can change business logic anytime by just modifying and deploying stored procedure by one click on SQL Server Management Studio and you also encapsulate all business logic in your stored procedure. 

JDBC just know name of your proc and as long as you don't change that, you can make changes without touching Java code. 

    1 comment :

    Anonymous said...

    nice comparison thanks

    Post a Comment