Tuesday 23 July 2013

Steps to configure Hibernate in Java Application

Steps to configure Hibernate in Java Application

  1. create configuration class object by loading configuration.xml file. We can load configuration.xml file by using configure() in configuration class.
         Configuration cf=new Configuration().configure("hibernate.cfg.xml"); 
now cf is able to read hibernate .cfg.xml using DOM parser.
2. create SessionFactory object using cf.
 SessionFactory sf=cf.buildSessionFactory();
Now sf is a hibernate SessionFactory interface object which maintains all the properties of hibernate.xml file.
step 3: create session object
Session session=sf.openSession();
step 4: create Transaction object by session object.

Transaction tx=session.beginTransaction();
step 5: use sesssion object for different CRUD operations.
step 6: finally commit the transaction by calling tx.commit();

classes and Interfaces can imported from the following packages

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

No comments:

Post a Comment