Hibernate vs Jdbc : Importance of Hibernate ORM over JDBC

Hibernate created by Gavin King, known as the best and dominated object/relational persistence (ORM) tool for Java developers. It provides elegant and innovative ways to simplify the relational database handling task in Java.

Hibernate is great at a lot of things, but its something that needs to be used appropriately. This post high lights usage and importance of hibernate vs Jdbc.

High Level Architecture of Hibernate

Hibernate-HighLevelArchitecture

1.Dealing with Objects

Hibernate is an ORM (Object Relational Mapping) tool. It is used to persist objects in a database. JDBC is used to store primitive types of data into database.

2.Connection Pool

Providing a connection pool for an application that uses Hibernate is pretty easy, as a matter of the fact Hibernate supports a variety of connection pooling mechanisms. If you are using an application server, you may wish to use the built-in pool (typically a connection is obtaining using JNDI).

If you can’t or don’t wish to use your application server’s built-in connection pool, Hibernate supports several other connection pools such as:

  1. c3p0 is distributed with Hibernate. http://sourceforge.net/projects/c3p0
  2. Apache DBCP is famous connection pool from Apache. http://jakarta.apache.org/commons/dbcp/
  3. Proxool is also distributed with Hibernate  http://proxool.sourceforge.net/

3.Caching

Hibernate provides two types of caching. JDBC doesn’t provide this feature, and to get this we need to implement our own logic for caching in JDBC.

Hibernate provides built in cache support as

  1. First Level Cache: The first-level cache is the Session cache and is a mandatory cache through which all requests must pass.
  2. Second Level Cache: Second level cache is an optional cache and first-level cache will always be consulted before any attempt is made to locate an object in the second-level cache. The second-level cache can be configured.

4.Transaction Support

Hibernate provides built in transaction management support using JTA.

5.Dialects

It allows you to perform Database activities with out bothering much about database changes. Dialect classes will take care of that to fire an appropriate query for respective database.

6.SQL queries for CRUD operations

No need to write most of the SQL scripts for persisting, deleting objects and parsing the result sets.

It is very easy  to make clean separation of Data Access Object Layer.

8.Removes Boiler plate coding

The core drawback of JDBC is that it doesn’t allow you to deal with objects directly. To the database you must  convert the objects to a relational format, for instance, if you want to persist new instance of the Student class to the database you must first convert Student class object to SQL statement that can be executed on the underlying database.

Similarly, when the rows are returned from the database you must convert ResultSet row into an instance of Student.

In Hibernate we directly work with objects with the API provided.

Hibernate is not tightly tied with any underlying database. Where as JDBC is tightly tied with the underlying database.

9.No need to master SQL

Hibernate is set of Java API, you don’t need to be a master of SQL language. You can treat table as Object, only Java knowledge is needed.

In case of JDBC, you need to learn database specific SQL language constructs.

Basic SQL queries are generated by Hibernate and we deal with objects.

In projects tables are created by DB team using SQL script and we don’t create the tables using Hibernate most of the times.

10.Query Tuning

No need to tune queries for performance. In case of Hibernate if you use Criteria queries, the Hibernate automatically tunes your query and return best result with performance.

We don’t require query tuning in case of Hibernate.

In case of JDBC you need to tune your queries till Oracle 8.x and 9.x.

11.Relationships

In XML file you can see all the relations between tables in case of Hibernate. Easy readability. It’s easy to implement relationships such as One-to-many, many-to-one and many-to-many etc.,

12. Lazy loading of Objects

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

In JDBC you don’t have such support.

for load() method in Hibernate lazy=true and for get() method lazy=false.

13.Versioning and Time stamping

Hibernate supports automatic versioning. and also supports automatic time stamping on database rows.

Low Level Architecture of Hibernate

LowLevelArchitecture-Hibernate

Happy Learning Hibernate 🙂

Feel free to express your opinion on this.

Leave a Comment