The general flow of Hibernate framework while interacting with the Database is:Example Program:
// creating the Configuration object
Configuration config = new Configuration();
config.configure();
// creating the SessionFactory object
SessionFactory sessionFactory = config.buildSessionFactory();
// creating the Session object
Session session = sessionFactory.openSession();
Transaction transaction = null;
try {
// creating the Transaction object
transaction = session.beginTransaction();
Person person = new Person();
person.setName("Ranga");
// save the Person object
long personId = (Long) session.save(person);
session.flush(); // commit the transaction
transaction.commit();
} catch(HibernateException ex) {
if(transaction != null) {
transaction.rollback();
}
} finally {
if(session != null) {
session.close();
}
} Step1: Creating the Configuration object
First we need to create the Configuration object. After creating Configuration object we need to call configure() method.
Configuration config = new Configuration();
config.configure();
public Configuration configure() throws HibernateException {
configure( "/hibernate.cfg.xml" );
return this;
}
While calling config.configure() method if you are not passing any argument configuration (*.cfg.xml) file then it will take default configuration file called hibernate.cfg.xml file.
This will load all configuration information like database information, dialect class, mapping files and etc..
Step2: Creating the SessionFactory object
By using configuration object only we can create the SessionFactory.
SessionFactory sessionFactory = config.buildSessionFactory();
Note: SessionFactory is an interface so directly we can't create the object and SessionFactoryImpl is the implemented class for the SessionFactory.
While calling config.buildSessionFactory() it is internally calls the SessionFactoryImpl class and creates the object that object returns the SessionFactory.
public SessionFactory buildSessionFactory() throws HibernateException {
// here some more statements
return new SessionFactoryImpl(
this,
mapping,
settings,
getInitializedEventListeners(),
sessionFactoryObserver
);
}
Step3: Creating the Session object
By using SessionFactory object only we can create the Session object.
Session session = sessionFactory.openSession();
Session is nothing but get the one physical connection. By using session we can do all CRUD operations.
Note: Like SessionFactory, Session is also an interface and which is implemented by SessionImpl class. In hibernate there are two ways to get the Session object i.e openSession() and getCurrentSession() [ See more info dif b/w openSession() and getCurrentSession()]. In the above statement we are calling sessionFactory.openSession() it gives the Session object.
public org.hibernate.classic.Session openSession() throws HibernateException {
// some more statements
return new SessionImpl(
connection,
this,
autoClose,
timestamp,
sessionLocalInterceptor == null ? interceptor : sessionLocalInterceptor,
settings.getDefaultEntityMode(),
settings.isFlushBeforeCompletionEnabled(),
settings.isAutoCloseSessionEnabled(),
settings.getConnectionReleaseMode()
);
}
Step4: Creating the Transaction object
By using Session object only we can create the Transaction object.
Transaction transaction = null;
transaction = session.beginTransaction();
Transaction is a logical unit. While working with insert, update, delete, operations from an 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. In order to begin a logical transaction in hibernate then we need to call a method beginTransaction() given by Session Interface.
Note: Transaction is also interface.
public Transaction beginTransaction() throws HibernateException {
// some more statements
Transaction result = getTransaction();
result.begin();
return result;
}
Step5: Now do any CRUD operation
Inside Session interface there are lot of methods is available to do CRUD operations. Some of the important methods is
Inserting object - save(), saveOrUpdate(), persist()
Selecting object - load(), get(), byId() [ this method is available in hibernate4]
Updating object - update(), saveOrUpdate()
Deleting object - delete()
In the above example i am doing insert operation. long personId = (Long) session.save(person);Step6: Commit or rollback the transaction
If the operations is successful then commit() the transaction otherwise rollback() transaction.transaction.commit();transaction.rollback();
Step7: Finally close the all resources like session, sessionFactory
So finally close the session session.close();