Hibernate






Hibernate framework architecture:-    


  Hibernate  Architecture:-  

         The Hibernate architecture includes many objects persistent 

object, session factory, transaction factory, connection factory, 

session, transaction etc.  There are 4 layers in hibernate architecutre

 java application layer, hibernate framework layer, backhand api 

layer and database layer.Let's see the diagram of hibernate 

architecture: 


 This is the high level architecture of hibernate with mapping file and configuration file

  Hibernate framework uses many objects session factory, session, transaction etc. alongwith existing Java API such as JOBC (Java Database Connectivity), JTA (Java Transaction API) and MIN (Java Naming Directory Interface).







Hibernate:-

    Hibernate is a frame work. Hibernate frame work is used to develop a java 

application to interact with database server. 

    A frame work is a piece of software this piece of software contains solutions from 

commonly repeatedly occurred problems occurs multiple projects.


Why Object Relational Mapping (ORM)?

When we work with an object-oriented systems, there's a mismatch between the object model and the relational database. RDBMSs represent data in a tabular format whereas object-oriented languages, such as Java or C# represent it as an interconnected graph of objects. Consider the following Java Class with proper constructors and associated public function:
public class Employee {
   private int id;
   private String first_name; 
   private String last_name;   
   private int salary;  

   public Employee() {}
   public Employee(String fname, String lname, int salary) {
      this.first_name = fname;
      this.last_name = lname;
      this.salary = salary;
   }
   public int getId() {
      return id;
   }
   public String getFirstName() {
      return first_name;
   }
   public String getLastName() {
      return last_name;
   }
   public int getSalary() {
      return salary;
   }
}
Consider above objects need to be stored and retrieved into the following RDBMS table:
create table EMPLOYEE (
   id INT NOT NULL auto_increment,
   first_name VARCHAR(20) default NULL,
   last_name  VARCHAR(20) default NULL,
   salary     INT  default NULL,
   PRIMARY KEY (id)
);
First problem, what if we need to modify the design of our database after having developed few pages or our application? Second, Loading and storing objects in a relational database exposes us to the following five mismatch problems.
MismatchDescription
GranularitySometimes you will have an object model which has more classes than the number of corresponding tables in the database.
InheritanceRDBMSs do not define anything similar to Inheritance which is a natural paradigm in object-oriented programming languages.
IdentityA RDBMS defines exactly one notion of 'sameness': the primary key. Java, however, defines both object identity (a==b) and object equality (a.equals(b)).
AssociationsObject-oriented languages represent associations using object references where as am RDBMS represents an association as a foreign key column.
NavigationThe ways you access objects in Java and in a RDBMS are fundamentally different.
The Object-Relational Mapping (ORM) is the solution to handle all the above impedance mismatches.

What is ORM?

ORM stands for Object-Relational Mapping (ORM) is a programming technique for converting data between relational databases and object oriented programming languages such as Java, C# etc. An ORM system has following advantages over plain JDBC
S.N.Advantages
1Lets business code access objects rather than DB tables.
2Hides details of SQL queries from OO logic.
3Based on JDBC 'under the hood'
4No need to deal with the database implementation.
5Entities based on business concepts rather than database structure.
6Transaction management and automatic key generation.
7Fast development of application.
An ORM solution consists of the following four entities:
S.N.Solutions
1An API to perform basic CRUD operations on objects of persistent classes.
2A language or API to specify queries that refer to classes and properties of classes.
3A configurable facility for specifying mapping metadata.
4A technique to interact with transactional objects to perform dirty checking, lazy association fetching, and other optimization functions.

Java ORM Frameworks:

There are several persistent frameworks and ORM options in Java. A persistent framework is an ORM service that stores and retrieves objects into a relational database.
  • Enterprise JavaBeans Entity Beans
  • Java Data Objects
  • Castor
  • TopLink
  • Spring DAO
  • Hibernate
  • And many more


Hibernate Caching Techniques:-

Caching is a Temporary memory or buffer which resides at client side and stores the results sent by server. When client generates same request for multiple number of times, the first request generated results will be stored in cache and this result will be used across the multiple requests. This reduces the round trips between the client and server. Since the result will be collected from cache that is available at client side.
Hibernate supports for 2 levels of cache: 
  1. Level one cache
  2. Level two cache.



Level One Cache:
Level 1 cache is inbuilt cache and it will be associated with hibernate session objects. Every session object of hibernate application contains one inbuilt level one cache.

Responsibilities of Level one cache:
a)      If select query executed for multiple no of times with in a session. Only one time query goes to database software gets the result, remaining all the times result will be gathered from cache.
b)      If one of pojo class object is modified for multiple no of times with in a transaction of session object, instead of sending update query for multiple number of times, all the changes done on the object wil be kept tracked and only one update query wil be generated reflecting all the changes at the end of the transaction.
The different ways to remove the level 1 cache from session

i)  Session.flush() –>  Flushes level one cache content to db software
ii)  Session.evict() –> Remove the content of level 1 cache
iii) Session.close() –> closes level 1 cache, before that it calls session.flush()

A hibernate client application can have multiple level1 caches because a hibernate application can have multiple hibernate session objects.
The data stored in level1 cache, level2 cache will be in full synchronization with table rows.

Level-2 Cache:

It is a configurable cache (Not a built in cache). Third party vendors are supplying supporting jar files for level 2 cache. Level2 cache is global cache and it is visible for all the session objects of the hibernate application.
When level-2 cache is enabled the results gathered for database software will be stored in both level 1 and level 2 caches.

sessionFactory.close() –> Destroys the session factory object and releases level 2 cache.
sessionFactory.evict(arga …) –> Removes pojo class object from session factory.
sessionFactory.evictQueries(args…) –> Cleans queries related data from cache.

If hibernate use same request as second request or different session objects then software tries to collects the results either from leve1/level2 caches.
There are different third party providers for level 2 cache:
  1. Swarm Cache
  2. OS Cache,
  3. EH Cache
  4. JBoss Tree Cache … etc.
Disadvantages of Hibernate:-

1) Steep learning curve.

2) Use of Hibernate is an overhead for the applications which are :

simple and use one database that never change
need to put data to database tables, no further SQL queries
there are no objects which are mapped to two different tables
Hibernate increases extra layers and complexity. So for these types of applications JDBC is the best choice.

3) Support for Hibernate on Internet is not sufficient.

4) Anybody wanting to maintain application using Hibernate will need to know Hibernate.

5) For complex data, mapping from Object-to-tables and vise versa reduces performance and increases time of conversion.

6) Hibernate does not allow some type of queries which are supported by JDBC. For example It does not allow to insert multiple objects (persistent data) to same table using single query. Developer has to write separate query to insert each object.



No comments:

Post a Comment