Java Hibernate Introduction

Java Hibernate

Introduction

·         Hibernate is an open source object relational mapping tool used in Java programming.

·         Hibernate allows data to be inserted, removed or changed in a database without paying a lot of attention to how it gets there.

·         Hibernate was founded by Mr. Gavin King, an Australian developer who needed to solve a problem and ended up creating Hibernate, an open source tool that is amazingly useful with JAVA.

·         The central idea of Hibernate is this: “JAVA programmers are used to creating POJOs [Plain Old Java Objects] in Java. So why do they need a second language like SQL, to put or apply (persist) those POJOs into the database? ”

·         Hibernate does object-relational persistence and querying. That means Hibernate puts whole objects into a relational database and pulls whole objects out of a relational database.

When Can I Use Hibernate?

Hibernate can be used in any Java program that needs to access a relational database. Hibernate does not need an application server to run.

When Should I Use Hibernate? 

There are main three points at where Hibernate is use if:
  • There is a non-trivial application
  • There are more than 10 tables in the relational DB.
  • The application uses an object-oriented Domain Model.
If the application does a lot of business logic—and does much more than just display tables of data on a webpage—then it is a good candidate for Hibernate.

Hibernate is best in applications with complex data models, with hundreds of tables and complex inter-relationships.

So Hibernate maps the java classes to the database tables. It also provides the data query and retrieval facilities that reduce the development time.

Advantages of Hibernate Framework

There are many advantages of Hibernate Framework. They are as follows:

1) Opensource and Lightweight: Hibernate framework is opensource under the LGPL license and lightweight.
 
2) Fast performance: The performance of hibernate framework is fast because cache is internally used in hibernate framework. There are two types of cache in hibernate framework first level cache and second level cache. First level cache is enabled bydefault.
 
3) Database Independent query: HQL (Hibernate Query Language) is the object-oriented version of SQL. It generates the database independent queries. So you don't need to write database specific queries. Before Hibernate, If database is changed for the project, we need to change the SQL query as well that leads to the maintenance problem.
 
4) Automatic table creation: Hibernate framework provides the facility to create the tables of the database automatically. So there is no need to create tables in the database manually.
 
5) Simplifies complex join: To fetch data form multiple tables is easy in hibernate framework.
 
6) Provides query statistics and database status: Hibernate supports Query cache and provide statistics about query and database status.

Java Hibernate Architecture

To use Hibernate, it is required to create Java classes that represents a table in the database and then map the instance variable in the class with the columns in the database.

Then Hibernate can be used to perform operations on the database like select, insert, update and delete the records in the table. 

Hibernate automatically creates the query to perform these operations.

For that Hibernate architecture has three main components:

1.      Connection Management
2.      Transaction management
3.      Object relational mapping

The following diagram describes the high level architecture of hibernate:



The above diagram shows that Hibernate is using the database and configuration Data (Hibernate.prpperties and XML Mappping) to provide persistence services (and persistent objects) to the application. 

(1)Connection Management: 

Hibernate Connection management service provide efficient management of the database connections. Database connection is the most expensive part of interacting with the database as it requires a lot of resources of open and close the database connection. 

(2)Transaction management: 

Transaction management service provides the ability to the user to execute more than one database statements at a time. 

(3)Object relational mapping: 

Object relational mapping is technique of mapping the data representation from an object model to a relational data model. This part of hibernate is used to select, insert, update and delete the records form the underlying table. 

When we pass an object to a Session.save() method, Hibernate reads the state of the variables of that object and executes the necessary query. Hibernate provides a lot of flexibility in use. It is called "Lite" architecture when we only use the object relational mapping component. 

While in "Full Cream" architecture all the three components object Relational mapping, Connection Management and transaction Management) are used.

Comments

Popular posts from this blog

પટેલ સમાજનો ઈતિહાસ જાણો : કોણ અને ક્યાંથી આવ્યા હતા પાટીદારો

Python HTML Generator using Yattag Part 1

Java Event Delegation Model, Listener and Adapter Classes