Home » Enterprise Java » hibernate » Create Hibernate SessionFactory example

About Ilias Tsagklis

Avatar photo
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Create Hibernate SessionFactory example

In this example we shall show you how to create a new SessionFactory example in Hibernate. To create a new SessionFactory example in Hibernate one should perform the following steps:

  • Create a new Configuration, that allows the application to specify properties and mapping documents to be used when creating a SessionFactory. Usually an application will create a single Configuration, build a single instance of SessionFactory and then instantiate Sessions in threads servicing client requests.
  • With configure() API method use the mappings and properties specified in an application resource named hibernate.cfg.xml. Then, with buildSessionFactory() instantiate a new SessionFactory, using the properties and mappings in this configuration. The SessionFactory will be immutable, so changes made to the Configuration after building the SessionFactory will not affect it.

In the code snippets that follow, you can see  the CreateHibernateSessionFactoryExample Class that applies all above steps and the hibernate.cfg.xml file, that holds all configuration for Hibernate.

package com.javacodegeeks.snippets.enterprise;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class CreateHibernateSessionFactoryExample {
	private static SessionFactory sessionFactory;
	public static void main(String[] args) throws Exception {
		sessionFactory = new Configuration().configure().buildSessionFactory();



<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC

  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"



  <!-- JDBC connection settings -->

  <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

  <property name="connection.url">jdbc:mysql://localhost/companydb</property>

  <property name="connection.username">jcg</property>

  <property name="connection.password">jcg</property>


  <!-- JDBC connection pool, use Hibernate internal connection pool -->

  <property name="connection.pool_size">5</property>

  <!-- Defines the SQL dialect used in Hiberante's application -->

  <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>

  <!-- Display and format all executed SQL to stdout -->

  <property name="show_sql">true</property>

  <property name="format_sql">true</property>

  <!-- Drop and re-create the database schema on startup -->

  <property name="hbm2ddl.auto">update</property>


This was an example of how to create a new SessionFactory example in Hibernate.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments