Steps to be followed to use Hibernate in Java
In our previous tutorial we have learned about Simple Hibernate Application Requirements. In this article we shall explore about the exact flow to use hibernate in a java application.
Following are the Steps to be followed to Use Hibernate in Java
1. Import Hibernate API
Import the following hibernate API.
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry;
2. Loading Configuration files
To load hibernate configuration xml, we need to create object of Configuration class and we need to call configure() method in that class.
Example:
Configuration cf = new Configuration().configure("hibernate.cfg.xml");
Once configure method is called, the following actions takes place:
- The configuration object cf will read configuration details from hibernate.cfg.xml file and Stores the data in different variables of a high level hibernate object called SessionFactory.
- Now this SessionFactory object sf contains all the data regarding the configuration file
So in java we get this configuration details by using the following code.
StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder(); srb.applySettings(cf.getProperties()); ServiceRegistry sr = srb.build(); SessionFactory sf = cf.buildSessionFactory(sr);
Note: Once we load the configuration file, the mapping file will be loaded automatically as we have configured the mapping xml in the hibernate configuration file.
3. Creating an object of session
To open a database connection in hibernate, we need to call openSession()
method of SessionFactory
, now assign this connection to Session interface.
Session session = sf.openSession(); //sf = Object of SessfionFactory
4. Create a logical transaction
Hibernate needs a logical Transaction to perform insert, update and delete operations into any database. In order to begin a logical transaction in hibernate we need to call beginTransaction()
method of Session Interface and assign it to Transaction interface.
Transaction tx = session.beginTransaction(); //session = Object of Session Interface
Note: For selecting an object from the database we do not require any logical transaction in hibernate.
5. Use Session Interface methods
We can use the methods given by Session Interface, to move the objects from java to database and from database to java
session.save(s) – Insert object ‘s’ into database
session.delete(s) – Delete object ‘s’ from database
session.update(s) – Update object ‘s’ in the database
session.load(s) – Select object ‘s’ from database
6. Commit Transaction
Now we need to call commit()
method of Transaction to perform commit operation using tx.commit()
;
7. Close Session and SessionFactory
As discussed earlier, when we open a session, a connection to database is created, so once the transaction is complete, we must close that connection via session.close()
code and finally we have to close the SessionFactory via sf.close()
code.
So the final flow of any hibernate application will be as below
Configuration SessionFactory Open Session Begin Transaction Session Commit Transaction Close Session and SessionFactory