In Hibernate, it was often to build a SessionFactoryand pull out a Session as follows:

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();

 

However, since Hibernate 4.x, this approach is deprecated. According to Hibernate 4 API docs, the Configuration class’ buildSessionFactory() method is deprecated and it recommends developers to use the buildSessionFactory(ServiceRegistry) instead. Here is the new recommended code snippet that builds the SessionFactorybased on a ServiceRegistry and obtains the Session:

Configuration configuration = new Configuration().configure();
ServiceRegistryBuilder registry = new ServiceRegistryBuilder();
registry.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = registry.buildServiceRegistry();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

Session session = sessionFactory.openSession();

 

The Configuration class’ configure() method loads mappings and properties from the convention resource file hibernate.cfg.xml which should be present in the classpath. It’s also very common to create a separate utility class for building the SessionFactoryas follows:

package net.codejava.hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {

	private static SessionFactory sessionFactory;

	public static SessionFactory getSessionFactory() {
		if (sessionFactory == null) {
			Configuration configuration = new Configuration().configure();
			ServiceRegistryBuilder registry = new ServiceRegistryBuilder();
			registry.applySettings(configuration.getProperties());
			ServiceRegistry serviceRegistry = registry.buildServiceRegistry();
			
			sessionFactory = configuration.buildSessionFactory(serviceRegistry);			
		}
		
		return sessionFactory;
	}

}

 

NOTE: As of 2014, the above code is deprecated. Here’s new version of the HibernateUtil class that is up-to-date:

package net.codejava.hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {
	private static SessionFactory sessionFactory;
	
	public static SessionFactory getSessionFactory() {
		if (sessionFactory == null) {
			// loads configuration and mappings
			Configuration configuration = new Configuration().configure();
			ServiceRegistry serviceRegistry
				= new StandardServiceRegistryBuilder()
					.applySettings(configuration.getProperties()).build();
			
			// builds a session factory from the service registry
			sessionFactory = configuration.buildSessionFactory(serviceRegistry);			
		}
		
		return sessionFactory;
	}
}

Here’s an example of using this utility class in a Hibernate application:

SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(myObject);

 

Other Hibernate Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Attachments:
Download this file (HibernateUtil.java)HibernateUtil.java[Utility class to build a SessionFactory]0.7 kB

Add comment

   


Comments 

#8Vishnu2020-05-30 07:24
Sir , Where should we mention the hibernate.cfg.xml file in this HibernateUtil class. It should be placed within configure() method right ?
Quote
#7Abhishek2017-08-10 07:37
also added method like getSession,beginTransaction,commitTransaction,closeSession
Quote
#6pavani2016-01-05 06:13
nice it very advisable
Quote
#5pavani2016-01-05 06:12
nice really helpefull..........
Quote
#4Nam2015-08-07 23:10
hi Raichand,

Can you please post your code here?
Quote