Wednesday, March 30, 2016

Hibernate Architecture Basic Introduction

Hibernate Architecture Basic Introduction

               Hibernate is one of the most popular Object/Relational Mapping (ORM) framework. It will provide developers to map normal java objects to relational structure of database.
Important components in Hibernate:

Entities: Entity is nothing but, it’s a simple java class(POJO) which is mapped to relational database by Hibernate.

Object Relational Meta data: There are two ways developers can choose how to map the entities to relational data base.   
    1. Legacy XML based configuration 
    2. Annotation ( Since jdk 1.5)
3   Hibernate Query Language (HQL):  HQL is similar to SQL but it’s an Object oriented query language and it will do all actions on persistent objects and its properties.


What persistent means in hibernate?
         
                 Persistent is the instances of your POJO classes and It will create Rows and Tables in the database. In future any modification happened on that POJO instance, and then it will be synchronized to database with the help of session scope.

Persistent Objects
Persistent Object

Hibernate Architecture
Hibernate Structure
Hibernate Architecture

Configuration:

             In any Hibernate application configuration object will be created at first during application initialization and it will be created only once. It’s available inside org.hibernate.cfg.Configuration.
Developer should pass hibernate.cfg.xml to configuration object and sub sequentially it will be configured.

SessionFactory:  

            Session Factory objects are implemented by using singleton pattern and It’s completely thread-Safe. Since Session Factory is Heavy Weight object, mostly developers will create this only once per application.

Why Session Factory is Heavy Weight Object?

           Since its having the details of DB connection information, Hibernate configuration information, mapping configuration files etc., so if you try to create number of session factory instance, it will make your application as Heavy Weight.

Session:

           Session is not a thread safe and light weight. All operations of persistent object will be done by Session object.

Transaction:  
           It allows the application to define some units of work. We can configure transaction management by our own way. So that, we have to write code for manually for commit and rollback. The below chart will explain simply.



Query:

            HQL is similar to SQL but it’s an Object oriented query language and it will do all actions on persistent objects and its properties. By calling createQuery() method of session object, you can obtain query like below.

Query query=session.createQuery ("from Employee");
Employee is your persistent class.

We can see deeply later on delete update etc.,

Criteria:

           Criteria is a powerful API, It allows developer to apply restriction rules and logical conditions. With the help of session object developer can apply criteria on persistent class through below.

Criteria criteria = session.createCriteria(Employee.class);

No comments:

Post a Comment