Hibernate – How to build SessionFactory

If you have been watching previous hibernate releases then you must have noticed that they have deprecated a lot of classes in quick succession. Deprecated classes are AnnotationConfiguration, ServiceRegistryBuilder and so on.

These classes are used to build session factory through the java code and if we use them in our application; they might work well but we will always see some annoying warnings about their depreciation.

In this tutorial, I am giving an example of building SessionFactory in Hibernate 5 without using deprecated classes mentioned above. I am using latest hibernate version i.e. Hibernate 5.4.17.Final, so you can make sure that you are using latest approach for building session factory.

1. Classes used in building SessionFactory

I have used following classes for building SessionFactory in hibernate.

  1. Configuration : In place of deprecated AnnotationConfiguration
  2. StandardServiceRegistryBuilder : In place of deprecated ServiceRegistryBuilder

How to build SessionFactory

Use below example code build the SessionFactory in hibernate. Feel free to tweak the code as per your need.

package com.howtodoinjava.hibernate.test;

import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class HibernateUtil 
   private static SessionFactory sessionFactory = buildSessionFactory();

   private static SessionFactory buildSessionFactory() 
         if (sessionFactory == null) 
            StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
            Metadata metaData = new MetadataSources(standardRegistry)
            sessionFactory = metaData.getSessionFactoryBuilder().build();
         return sessionFactory;
      } catch (Throwable ex) {
         throw new ExceptionInInitializerError(ex);

   public static SessionFactory getSessionFactory() {
      return sessionFactory;

   public static void shutdown() {

Where the configuration file is:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      <property name="hibernate.connection.driver_class">org.h2.Driver</property>
      <property name="hibernate.connection.url">jdbc:h2:mem:test</property>
      <property name="hibernate.connection.username">sa</property>
      <property name="hibernate.connection.password"></property>
      <property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
      <property name="show_sql">true</property>
      <property name="hbm2ddl.auto">create-drop</property>
      <mapping class="com.howtodoinjava.hibernate.test.dto.EmployeeEntity"></mapping>

I hope the above information will help you when migrating from hibernate 3.

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

6 thoughts on “Hibernate – How to build SessionFactory”

Leave a Comment


A blog about Java and related technologies, the best practices, algorithms, and interview questions.