Hibernate Tutorial

James Gosling

Hibernate tutorial provides basic and advanced concepts of Hibernate. Our Hibernate tutorial is designed for beginners and professionals.

Hibernate is an open source framework. It is provided by Hibernate is free software that is distributed under the GNU Lesser General Public License. It is written in Java and currently used by Google, Facebook, LinkedIn, Yahoo, Twitter, JavaTportal etc.

This hibernate tutorial provides in-depth concepts of Hibernate Framework with simplified examples. It was started in 2001 by Gavin King. The stable release of Hibernate till July 16, 2014, is hibernate 4.3.6. It is helpful for beginners and experienced persons.

What is Hibernate Framework

Hibernate is an ORM framework which manages the persistence of object on behalf of applications.Hibernate is a non-invasive framework, means it wont forces the programmers to extend/implement any class/interface, and in hibernate we have all POJO classes so its light weight.

Before the concept of ORM JDBC was used for persistence :

hibernate tutorial, An introduction to hibernate

Disadvantage/Limitations:

  1. Connection need to created and managed by the application.
  2. Queries need to be generated for persistence operations by the applications.
  3. Mapping of objects to query parameters and ResultSet to object is to be managed by the application.

limitations of model1 are remove by model2 of persistence which uses ORM frameworks for managing persistence.

hibernate tutorial

Advantages:

  1. Connections are created and managed by the Framework.
  2. Queries are generated for persistence operations by the framework.
  3. Mapping of objects to query parameters and ResultSet to objects is managed by framework.
  4. Performance of persistence operation is improved by caching of persistence object.

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.


In order to use hibernate following terms need to be understood:

  1. Persistence object an object which contains data that can persisted to database is called Persistence object.
  2. Session: is an interface provided by hibernate this interface exposes methods for managing the persistence of persistence object. implementation of this interface is provided by the framework. This session object is required by the application for performing persistence operations. This object as transactional scope thats it is created only for the duration of performing persistence operations.
  3. SessionFactory: is an interface of the framework it provide factory method for creating session objects as well as methods for managing mapping and configuration information and second level cashing. Implementation of this interface is provided by the framework an object of type SessionFactory is created and used through out the application that's this objects as application scope.
  4. Configuration: configuration is framework provided class which is responsible for maintaining configuration information in object forms. (Each ORM framework required some database specific information. Such as driver Class, URL username, Password etc. this information is called configuration information. Conventionally configuration information is provided to the framework in xml format with the help of file name hibernate.cif.xml)
  5. MappingInformation: ORM frameworks requires information of objects and there relation to database tables. This information is used in generating queries and ResultSet to object mapping. Conventionally mapping information is provided in xml format. For each persistence class in xml file is created which is conventionally name as .hbm.xml

Hibernate Examples


Hibernate One-To-One Mapping Tutorial

You can see how to do one-to-one mapping using hibernate.


Hibernate One-To-Many Mapping Tutorial

You can see how to do one-to-one mapping using hibernate.


Hibernate Many-To-Many Mapping Tutorial

You can see how to do many-to-many mapping using hibernate.


Hibernate Component Mapping Tutorial

You can see Hibernate Component Mapping Tutorial how to do component mapping using hibernate.


Hibernate Mapping One-to-Many Using Annotations

You can see how to do one-to-many mapping using hibernate annotations.


Hibernate Many-To-Many Mapping Using Annotations Tutorial

You can see how to do many-to-many mapping using hibernate annotations.


Hibernate Component Mapping Using Annotations Tutorial

You can see how to do component mapping using hibernate annotations.


Hibernate Many-To-One Mapping Tutorial

You can see how to do many-to-one mapping using hibernate.


Hibernate Example

You can see how to create a simple example using the Eclipse IDE and Hibernate Tools plug-in. HSQLDB is used for persisting data in the back-end.


Hibernate Annotations Tutorial

You can see how to create a simple example using the Hibernate Annotations.


Hibernate Mapping One-to-One Using Annotations Tutorial

You can see how to do many-to-one mapping using hibernate annotations.


Hibernate Mapping Many-to-One Using Annotations Tutorial

You can see how to do many-to-one mapping using hibernate annotations.


Next Concept Architecture