Advantages of Hibernate Framework

Traditional way of storing objects in a database table, is to break up object into primitive data types, and store each primitive data in a table column. Application developer need to spend more time on writing mapping source code between class data members and table columns,etc… Situation worsens when relationship exists between objects, which need to be stored in database.

Hibernate provides solution for above problems.

Below is anatomy of Java Application, which uses Hibernate Framework.

hibernate-advantages
hibernate-advantages

1.Reduces size of code, hence development of Application is faster, which in turn controls cost of overall project.
When Hibernate is used, you need not write code to perform below routine activities
Connecting to database, retrieve individual columns from database table and creating object out of it , and vice versa.

2.Transaction Control: Hibernate has built in Transaction Management. A Transaction can be started, commited or rolled back by using Hibernate.
3.Automatic creation and updation of tables or columns. Required tables and columns are automatically created or updated, by provding hbm2ddl property in hbm file. Hibernate gets details of table names, column names, type,etc… from Hibernate Mapping file(i..e hbm.xml)

4.Provides HQL, Hibernate Query language, using which developer need to provide queries in terms of class names, and data members, instead of focusing on tables and columns.

5.Hibernate Caching, caches objects, so that when same object(s) are queried next, object from cache is returned, and database hit is avoided. This improves overall performance of Application.

In you may find, few program statements used again and again, like openSession(), beginTransaction(), commit(), rollBack(), close(),etc…which can be avoided multiple times, by better designing your application. Another alternative to further reduce Hibernate code, is to use combination of Spring and Hibernate.

Other facts:
Hibernate Framework further depends on JDBC, to interact with underlying database. Hence JDBC driver jar file need to be added to projects using Hibernate.

Such Framework is referred as ORM(Object Relational Mapping)

You may also like to read:
Mapping Type in HIbernate Mapping file
DDL with JDBC Statments
How many types of JDBC Drivers exist

SQL DDL with JDBC Statements

There are five different types of SQL statements. SQL stands for Structured Query Language.
In this post we are considering MySQL as example.

#1.DDL, Data Definition Language:
Below SQL statements are considered as DDL
CREATE DATABASE
CREATE TABLE
ALTER
DROP

#2.DML, Data Manipulation Language:
Below SQL statements are considered as DML
INSERT
UPDATE
DELETE
TRUNCATE

#3.DQL, Data Query Language:
Below SQL statement are considered as DQL
SELECT

#4.TCL, Transaction Control Language:
Below SQL statements are considered as TCL
COMMIT
ROLLBACK

#5.DCL, Data Control Language:
Below SQL statements are considered as DCL
GRANT
REVOKE

Can DDL statements be invoked from JDBC?
Yes, DDL statements like CREATE, ALTER, DROP can be executed from JDBC Statement. MySQL JDBC jar need to be added to Project, for this Program to connect to Database.

Below is an example, which is self explanatory

import java.sql.*;

public class DDLJDBC {

    public static void main(String[] args)
    {
    try
    {
        Class.forName("com.mysql.jdbc.Driver");

        System.out.println("Connecting to database...");

        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/mondaybt",
                "myuser","mypassword");

        System.out.println("Creating Statement...");

        Statement stmt = con.createStatement();
        stmt.execute("create table institute(id bigint, sname varchar(50), saddr varchar(50))");

        System.out.println("Creating Table...");
        stmt.execute("alter table institute add column sfather_name varchar(100)");

        System.out.println("Altering Table...");

        stmt.close();
        con.close();
    }
    catch(SQLException se)
    {
        se.printStackTrace();
    }
    catch(Exception ep)
    {
        ep.printStackTrace();
    }
    }
}

You may also like to Read:
What is JDBC?

Different Types of JDBC Drivers

These days almost no Enterprise Application exist without Database, to store data. And for Java Applications to interact with underlying databases, JDBC Driver is required.

JDBC Driver acts as an interface between Java Application and Database server, as shown in below picture.

JDBC Driver
JDBC Driver

Due to historical reasons there are four different types of JDBC Drivers.

Type 1 JDBC Driver: JDBC-ODBC Bridge driver (Bridge): An ODBC driver uses the Open Database Connectivity (ODBC) interface by Microsoft that allows applications to access data in database management systems (DBMS) using SQL as a standard for accessing the data.  JDBC-ODBC bridge further interacts with ODBC driver, which further interacts with database. These Type of drivers were used during initial versions of Java in 90’s. There is performance hit as JDBC-ODBC bridge causes additional overhead, when Java app interacts with database.

Type 2 JDBC Driver: Native-API/partly Java driver (Native): These drivers again use a Java (JDBC) API, and further interact with local shared libraries in C or C++, which really interact with the Database Server. This approach uses Java Native API feature to interact with C/C++ code. When performance tuning is done in C/C++ libraries there is a chance that Type 2 Drivers can perform similar or better than Type 4.

Type 3 JDBC Driver: AllJava Net-protocol driver (Middleware): Here Java Application send request to Network Proxy Server, which further interacts with Database Server.

Type 4 JDBC Driver: All Java Native-protocol driver (Pure). These type of Driver are completely implemented in Java, and hence they are platform independent. Currently, Type 4 drivers are being widely used. Type 4 Driver for each database are provided by respective Database vendor, as a JAR file. This JAR file need to be added to Java Project, which needs to interact with database. For eg. MySQL  JDBC Driver is provided in below link  MySQL JDBC Driver Download(Jar)
This jar file can be used on any platform.

Reason to have above different types, is due to performance and portability reasons.

Type 1 has worst Performance and Portability, while Type 4 has better Performance and Portability, comparing with other Driver Types.

Currently Type 4 Drivers are used widely.