Hibernate configuration – create Hibernate SessionFactory, Session Object

Like Tweet Pin it Share Share Email

Let us learn three basic ways to do hibernate configuration in this tutorial. Further completion of hibernate configuration, we can easily get hibernate session object.

Hibernate is a ORM tool. Using Hibernate we can insert, update and delete records from Database. 

Hibernate configuration

Before inserting, updating or deleting any object from Database using Hibernate, we need to tell Hibernate about our application settings or configuration. 

For example what kind of database are we using? How do we connect to the database? What kind of objects are we want to persist?

You can do Hibernate configuration in three ways:

Idea 1. Hibernate configuration programmatically using API:

While configuring Hibernate programmatic way we use Hibernate provided API to load the hbm.xml files, load the database driver and specify the database connection details.

We will demonstrate this programmatic configuration by taking one java standalone example. So here is one example to configure Hibernate session by using hibernate API.

Eclipse Project Image for this example:

Hibernate configuration using hibernate API
Hibernate Configuration Workspace

Image shown above is eclipse project image for the programmatic configuration of hibernate.

Moreover you can create one simple java project with hibernate capabilities as shown in the image. Here our java project name is “programmaticallyHibernateMySql”.


package com.hibernate.dbConnection;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory getSessionFactory() {
// create configuration using hibernate API
Configuration configuration = new Configuration();
configuration.setProperty("hibernate.connection.username", "root");
configuration.setProperty("hibernate.connection.password", "mysql");
return configuration.buildSessionFactory();
public static void main(String[] args) {
// getting session factory
SessionFactory sessionFactory = getSessionFactory();
.println("Session factory object created : " + sessionFactory);
Session session = sessionFactory.openSession();
try {
System.out.println("Session object created : " + session);
// We can use this session object for doing CRUD (inserting,
// updating and deleting rows)
} catch (Exception e) {
} finally {

Output :

Once we will run this java program the output of the code will look like below give texts. In case your configuration is incorrect you will get an exception screen.

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Session factory object created : org.hibernate.impl.SessionFactoryImpl@192b996
Session object created : SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[]])

This SessionFactoryImpl@192b996 may be different for you. For me also it is changing every time for new output.

Idea 2. Hibernate configuration using XML :

In fact XML configuration is the best and standard way to configure hibernate session.

In this case we need to provide database connection parameters, database driver load details, objects to be persist details in hibernate.cfg.xml.

Please read :   “how to create Hibernate Session Object using hibernate.cfg.xml”.

Idea 3. Hibernate configuration using Properties file :

In this method we use .properties file to configure hibernate session. We also provide all the database credentials in a properties file. 

This is similar to the XML configuration but uses a .properties file. The default name is hibernate.properties.

Comments (1)

  • Your style is so unique in comparison to other folks I’ve
    read stuff from. Many thanks for posting when you’ve got the opportunity, Guess
    I will just book mark this blog.


Leave a Reply

Your email address will not be published. Required fields are marked *