Hibernate is an ORM framework. Object Relational Mapping is a technique to map data of Object Domain into data of Relational Domain. In each Application, Application developers need to persist data into databases and need to fetch persistent data from databases. Java provides JDBC API for this task.
o The problem with JDBC is that an Application Developer need to perform all database related task such as opening the connection, formulating the query, executing the query, closing the connection, etc. i.e. Application Developer need to have knowledge of both Object Domain as well as Relational Domain.
Main functionality of an ORM Framework is to persist objects and their relations into the database on behalf of Application Developers. For this ORM Frameworks require configuration & mapping information which can be provided in the form of XML or Annotations.
o Configuration
Configuration object is used by the Hibernate to store Configuration Information in object form. Configuration Information represents data required by Hibernate to connect to a DB & information of classes that are to be managed by Hibernate.
o SessionFactory
It is a factory class which is used by Application Developer to create session.
Apart from this, it can also be used to manage 2nd level caching of persistent
object.
o Session
This is the main interface with which Application Developer interact. This
interface provides methods for persisting object, creating transactions & query object.
For each user a different session is created.
o Transaction
This interface is used by Application Developer to transact execution of queries.
o Query
This interface is used by the Application Developer to execute HQL (Hibernate
Query Language) Queries. HQL is an Object based version of SQL used by
Hibernate.
Steps required to persist objects using Hibernate :-
o First Step
▪ The persistent class objects of which are to be persisted is to be created. A persistent class has following characteristics –
It has a unique identifier which is used by Hibernate to uniquely identify objects.
It provides getter & setter methods for all its properties.
o Second Step
▪ A mapping file need to be created to specify information of database tables in which data of objects is to be persisted.
o Third Step
▪ A configuration file that contains database properties & references of mapping files need to be created.
o Fourth Step
▪ A configuration object is created.
o Fifth Step
▪ Using the configuration object a SessionFactory is obtained.
o Sixth Step
▪ Using the SessionFactory, Session object is obtained.
o Seventh Step
▪ Using the Session, objects are persisted.
0 Comments
Post a Comment