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

Like Tweet Pin it Share Share Email

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="hbm2ddl.auto">update</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>

Create one Java Class (HibernateUtils.java)

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.

Comments (5)

  • When I am executing this project I am getting this erros

    Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: org/dom4j/DocumentException
    Exception in thread “main” java.lang.ExceptionInInitializerError
    at com.test.HibernateUtils.buildSessionFactory(HibernateUtils.java:15)
    at com.test.HibernateUtils.(HibernateUtils.java:8)
    Caused by: java.lang.NoClassDefFoundError: org/dom4j/DocumentException
    at com.test.HibernateUtils.buildSessionFactory(HibernateUtils.java:12)
    … 1 more
    Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    … 2 more

    • Thanks for your valuable feedback. You are getting error while creating connection itself. Please give credentials properly 1. Database (Oracle or MySql) 2. User Name and Password as per your Database. 3. Please test classpath properly (jars). Please send me your project completely to me at tarunkumrs@gmail.com. I will check and let you know. I should work fine.

  • it work for me. look like you miss dom4j

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


Leave a Reply

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