This article was last updated on : November 4th, 2016 at 3:50 am

Creating Hibernate Session Object using hibernate.cfg.xml file, MySql

Creating Hibernate Session Object using hibernate.cfg.xml file, MySql is easy. First we need to create SessionFactory and then we can get Session Object from it. This is a step by step guide to create org.hibernate.SessionFactory object in Hibernate 3.0 and get org.hibernate.Session object from it.

To do this example we need properly installed MySql database in windows, hibernate.cfg.xml file and one java test class.

Before starting I am assuming that you have set up Hibernate in eclipse. In case you have not installed please read setup hibernate 3.0 development environment in eclipse IDE.

So Please follow the steps to complete this example.

Create a hibernate.cfg.xml file

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">mysql</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="">update</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>

Create one Java Class (

package com.hibernate.dBConnection;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
public class HibernateUtils {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
return new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
public static SessionFactory getSessionFactory() {
return sessionFactory;
public static void main(String[] args) {
SessionFactory factory = getSessionFactory();
System.out.println("Session factory object created : " + factory);
Session session = factory.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 {

Now if you are using eclipse or you are doing this example manually using Command Prompt. Please make sure database services are in running mode. Also make sure the file architecture in eclipse.

** In case file hierarchy is not correct you may get error in loading hibernate.cfg.xml. means you will not get Session Object created**

So Please below eclipse image which is showing the hierarchy of this example.

Creating Hibernate Session Object using hibernate.cfg.xml file, MySql

Creating Session Object Using hibernate.cfg.xml in Hibernate 3.0

We can see the SessionFactory object in the output like this :

Creating Hibernate Session Object Using hibernate.cfg.xml file

This image is showing the hibernate session object in output

This is all about creating Hibernate Session object using hibernate.cfg.xml file, MySql. Please let me know if you have any error while compiling or while running this example.

Share this post:

Recent Posts


  1. Avatar for Tarun Singh
    Sudeep says:

    When I am executing this project I am getting this erros

    Initial SessionFactory creation org/dom4j/DocumentException
    Exception in thread “main” java.lang.ExceptionInInitializerError
    at com.test.HibernateUtils.buildSessionFactory(
    at com.test.HibernateUtils.(
    Caused by: java.lang.NoClassDefFoundError: org/dom4j/DocumentException
    at com.test.HibernateUtils.buildSessionFactory(
    … 1 more
    Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException
    at Method)
    at java.lang.ClassLoader.loadClass(
    at sun.misc.Launcher$AppClassLoader.loadClass(
    at java.lang.ClassLoader.loadClass(
    … 2 more

  2. Avatar for Tarun Singh
    nathan says:

    it work for me. look like you miss dom4j

  3. Avatar for Tarun Singh
    Uday says:

    I was struggling while creating Hibernate Session Object. This website might help me to find some good suggestions!

Leave a Comment