/Hibernate & JDBC

Hibernate & JDBC

Hibernate & JDBC

  •  JDBC “Java Database Connectivity” : It’s the tool by which developers can connect, query and update database using the SQL, developer has to write code to map an object model’s data representation to a relational data model and its database schema, JDBC is Java version of ODBC.
  •  Hibernate : It’s a Flexible ORM soultion for JAVA “Object-Relational Mapping” to map Java classes to database tables, that allows java developers to develop persistent classes by object-oriented using association, inheritance and polymorphism mechanisms, developer does not need to write code explicitly to map database tables tuples to application objects during interaction with RDBMS.

* Note that :
JDBC simples the interaction with RDBMS “Relational database management systems” ,

So that if you want to access database using JAVA application you have to :-

 —  Load the JDBC driver because this driver actually communicates with the database, Then Open connection to database.
Create JDBC Statement object, This object contains SQL query Then execute statement which returns resultset.
 ResultSet : contains the tuples of database table as a result of SQL query.
Then send SQL queries to database using JDBC drivers and work on the returning results from database.
Close the Database connection.
JDBC uses two techniques to communicate with database:
First technique :-

  1.   The JDBC driver connects to database.
  2.   Executes SQL statements.
  3.   Results are sent back from driver to driver manager and finally to the application.

Second technique :-

  1.   The JDBC driver communicates with ODBC driver.
  2.   Then ODBC driver executes SQL query
  3.   Then results are sent back to JDBCdriver to driver manager and then to application.ODBC “Data Source” >> JDBC Servlet >> Browser.

Mapping of Java objects with database tables and vice versa conversion is done by the developer “manually” with lines of code.
JDBC supports “only” native Structured Query Language (SQL), Developer has to find out the efficient way to access database, to select query from a number of queries to perform same task.

With JDBC ..
Mapping between Java objects and database tables is done manually.
It is developer’s responsibility to handle JDBC result set and convert it to Java objects through code to use this persistent data in application, So if table changed or database changed then It is developer’s responsibility to change object structure and to change code written to map table-to-object & object-to-table.

Spring’s JDBC framework makes code easy to read and manages connections.

* Note that :

Hibernate is more flexible, powerful and high
performance object relational persistence and query service ,

So that if you want to access Database using Hibernate technique, you will find that :

1. Load the Hibernate configuration file and create configuration object. It will automatically load all mapping files.

Hibernate opens connection to database by itself.

2. Create HQL query “Hibernate Query Language”, Hibernate converts HQL “Hibernate Query Language” statements to database    data   and receives result set – (independent from type of database) that is expressed in SQL like syntax and includes full support for polymorphic queries.

** Hibernate also supports native SQL statements.
Hibernate also selects an effective way to perform a database manipulation task for an application,
Then execute query to get list containing Java objects.

3. Hibernate maps of these database data to Java objects which are directly used by Java application “Automatic Mapping”.

With Hibernate ..
Hibernate provides this mapping “itself“, Actual mapping between tables and application objects is done in XML files, So if there is change in Database or in any table then the need will be to change XML file properties.

  •   Hibernate uses JDBC to connect to the database and execute the dynamic SQL that it generates.
  •   Hibernate is nothing more than a SQL generation engine – it uses bjects and mappings to generate SQL, instead of making writing it by hand.

Now we have these question .. Why Hibernate .. So why not !?

  Why Hibernate :

  1.   Open-Source, Zero-Cost Product License.
  2.   Less code in Hibernate than JDBC.
  3.   Hibernate is standard ORM “Object-Relational Mapping” solution.
  4.   Database Independent.
  5.   Hibernate is layers architecture, So you only use whatyour appllication need.
  6.   Support for Query Language,While JDBC supports only native Structured Query Language “SQL“, Hibernate provides a powerful query language Hibernate Query Language (independent from type of database) and supports native SQL statements.
  7.   Updates and Maintenance, Hibernate reduces lines of code by update object table mapping itself and returns result to the application in Java objects, While is developer’s responsibility to handle JDBC result set and convert it to Java objects and mapping between Java objects and database tables is done manually.

  Why not Hibernate :

  1. Debugging and Performance are difficult.
  2. Hibernate is slower than JDBC as it executes lots of SQL statements in runtime.
  3. Internet Support for Hibernate is not powerful.
  4. Hibernate is an overhead for the applications which are simple and use one database that will never change or there are no objects will map between two different tables.
  5. Need to know Hibernate to update application using Hibernat.