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() 
   {
      try 
      {
         if (sessionFactory == null) 
         {
            StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
                  .configure("hibernate.cfg.xml").build();
            
            Metadata metaData = new MetadataSources(standardRegistry)
                  .getMetadataBuilder()
                  .build();
            
            sessionFactory = metaData.getSessionFactoryBuilder().build();
         }
         return sessionFactory;
      } catch (Throwable ex) {
         throw new ExceptionInInitializerError(ex);
      }
   }

   public static SessionFactory getSessionFactory() {
      return sessionFactory;
   }

   public static void shutdown() {
      getSessionFactory().close();
   }
}

Where the configuration file is:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
      <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>
   </session-factory>
</hibernate-configuration>

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

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

6 thoughts on “Hibernate – How to build SessionFactory”

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.