Hibernate is an Object relational framework (ORM framework), wherein it maps java entities to relational databases. It simplifies the mapping between your java objects and the relational database tables on top of jdbc.
Why hibernate? – In traditional approach, where you used to have jdbc for such type of mapping, you had some code which you always need to write when there is some operation between java objects and relational database. For eg: save,delete,update java objects to relational db or viceversa. This required jdbc to write code to open a connection, perform the operation, commit,flush and the close the connection. So this is a kind of boiler plate code. Hibernate takes care of all this internally.
Second advantage using hibernate is caching. We have first level cache,second level cache (this we will discuss in detail further) where objects can be cached when first request is made and then for subsequent request we can use this cached data instead of hitting databases.
Other uses of hibernate are – it supports inheritance, polymorphism, simplified integration with Spring.
All these above features we will discuss in detail below. Stay tuned…
Hibernate is also a JPA implementation. Java Persistance API which is also an ORM framework is not an implementation itself. It has hibernate, toplink,eclipselink as its implementations.
Hibernate consists of SessionFactory interface which is one per application. Now what is SessionFactory interface? SessionFactory is used to load all the hibernate configuration we do in our application and then create session objects .
Session objects are further used to interact with Java entity objects and perform operations on them . For reference consider below code.
|Configuration cfg = new Configuration.configure(“hibernate.cfg.xml”); //load hibernate.cfg.xml
SessionFactory sf = cfg.buildSessionFactory(); // create SessionFactory
Session s = sf.openSession(); // create session object
s.beginTransaction(); // start transaction
///perform operations on entities such as save,persist,update,delete
s.commit(); //commit transaction
Above is sample code which tells how does hibernate works.
Hibernate has below configurations which are necessary in each application where hibernate is used
- hibernate.cfg.xml — consists of configuration related to database connections
- hibernate.xml — whichever entities/java objects or classes are used are mentioned here
Caching in hibernate – Hibernate has default first level cache, where in say if a session object has started a transaction and then it gets some data from database, it gets stored in first level cache , and if again in the same transaction it again wants to get data it can get the data from the cache without hitting the database. Hibernate has two important methods which gets the data from database
1. load()- it first hits the cache and checks if data exists, it gets the data otherwise it hits the database.
2. get()- it always hits the database to get the data.
Hibernate has second level cache which should be enabled to make it work. It uses a common cache for all the session object of a session factory. It is useful if you have multiple session objects from a session factory.
SessionFactory holds the second level cache data. It is global for all the session objects .
For more information on caching in hibernate refer caching in hibernate