Steps To Use Hibernate In Any Java Application

Hello mates, this is the exact flow of any hibernate application. so you must put little more concentration while you are reading this post, to understand better.

Whether the java application will run in the server or without server, and the application may be desktop or stand-alone, swing, awt, servlet…whatever, but the steps are common to all.

To work with hibernate we don’t require any server as mandatory but we need hibernate software (.jar(s) files)

Follow The Steps:

1. Import the hibernate API, they are many more, but these 2 are more than enough…

import org.hibernate.*;
import org.hibernate.cfg.*;

2. Among Configuration, Mapping XML files, first we need to load configuration XML, because once we load the configuration file, automatically mapping file will be loaded as we registered this mapping XML in the configuration file.

So to load configuration XML, we need to create an object of Configuration class, which is given in org.hibernate.cfg.*;  and we need to call configure() method in that class, bypassing XML configuration file name as parameter.

Eg:

Configuration cf = new Configuration();
cf.configure(“hibernate.cfg.xml”);

Here our configuration file name is your choice, but by default am have been given hibernate.cfg.xml,  so once this configuration file is loaded in our java app, then we can say that hibernate environment is started in our program.

So once we write the line_ cf.configure(“hibernate.cfg.xml”), configuration object cf will read this XML file hibernate.cfg.xml, actually internally cf will use DOM parsers to read the file.

Finally…

  • cf will read data from hibernate.cfg.xml
  • Stores the data in different variables
  • And finally, all these variables are grouped and create one high level hibernate object we can call as SessionFactory object.
  • So Configuration class only can create this SessionFactory object

likeSessionFactory sf =  cf.buildSessionFactory();

Actually, SessionFactory is an interface, not a class, and SessionFactoryImpl is the implemented class for SessionFactory, so we are internally creating an object of SessionFactoryImpl class and storing in the interface reference, so this SessionFactory object sf contains all the data regarding the configuration file so we can call sf as heavy weight object.

3. Creating an object of the session,

  • The session is an interface and SessionImpl is implemented class, both are given in org.hibernate.*;
  • Whenever a session is opened then internally a database connection will be opened, to get a session or open a session we need to call the openSession() method in SessionFactory, it means SessionFactory produces sessions.

Session session = sf.openSession();

sf = SessfionFactory object

4. Create a logical transaction

While working with insert, update, delete, operations from a hibernate application onto the database then hibernate needs a logical Transaction, if we are selecting an object from the database then we do not require any logical transaction in hibernate.  To begin a logical transaction in hibernate then we need to call a method beginTransaction() given by Session Interface.

Transaction tx = session.beginTransaction();

the session is the object of Session Interface

5. Use the methods given by Session Interface,  to move the objects from application to database and  from database to application

session .save(s) Inserting object ‘s‘ into database
session.update(s) Updating object ‘s‘ in the database
session.load(s) Selecting object ‘s‘ object
session.delete(s) Deleting object ‘s‘ from database
  • So finally we need to call commit() in Transaction, like tx.commit();
  • As I told earlier,  when we open sessions a connection to the database will be created right, so we must close that connection as a session. close().
  • And finally close the SessionFactory as sf.close()
  • That’s it.., we are done.

Final flow will be______________

Configuration

SessionFactory

Session

Transaction

Close Statements

Related Articles

post a comment