Hibernate 5 build SessionFactory Example

In this tutorial I will show you how to build SessionFactory in Hibernate 5. Method of building a SessionFactory is different from the previous version of Hibernate.

Hibernate 5 build SessionFactory Example

Example of building SessionFactory in Hibernate 5

In this tutorial we are going to see the new way of building SessionFactory in Hibernate 5. Hibernate 5 comes with major changes and many new features. These changes also includes the way of building SessionFactory.

In Hibernate based application the instance of org.hibernate.SessionFactory in created at the startup of the application and its instance is closed at the end of the applications. Today we will see how to create the org.hibernate.SessionFactory instance at startup of application in Hibernate 5.

In Hibernate 5 StandardServiceRegistryBuilder class is used to create the instance of ServiceRegistry.

Following are the constructors of StandardServiceRegistryBuilder:

  • StandardServiceRegistryBuilder() - This constructor is used to create a default builder.
  • StandardServiceRegistryBuilder(BootstrapServiceRegistry bootstrapServiceRegistry) - This constructor is used for creating the builder with the specified bootstrap services.
  • StandardServiceRegistryBuilder(BootstrapServiceRegistry bootstrapServiceRegistry, LoadedConfig loadedConfigBaseline) - This is used to create a builder by specifying bootstrap services.

Example of building SessionFactory in Hibernate 5

You can use any of the above constructor in your application.

But this old way of creating SessionFactory won't work in Hibernate 5.

Building Hibernate 5 SessionFactory

Following is the code for building the SessionFactory object in Hibernate 5:

static {
   try {
	StandardServiceRegistry standardRegistry = 
       new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
	Metadata metaData = 
        new MetadataSources(standardRegistry).getMetadataBuilder().build();
	sessionFactory = metaData.getSessionFactoryBuilder().build();
   } catch (Throwable th) {
	System.err.println("Enitial SessionFactory creation failed" + th);
	throw new ExceptionInInitializerError(th);
  }
}

In Hibernate 5 StandardServiceRegistryBuilder class is used with the default constructor. In the above code you can see how to provide the  hibernate.cfg.xml to create SessionFactory in the application. The SessionFactory object is the singleton object in the application and it is created at the startup of the application.

During the Hibernate 5 bottstrap process system reads the configuration from hibernate.cfg.xml  and finally creates the SessionFactory object which can be used by application to perform CURD operation on the underlying database.

Video explanation of building the SessionFactory and creating first Hibernate example:

View the steps and run this example at Hibernate 5 Annotation Example. You will also find the source code of the project on the page Hibernate 5 Annotation Example.

Hibernate Tutorials