Skip to content

New & Improved: Getting A Hibernate SessionFactory

Hibernate 4.3.0 changes and deprecates a few things. One of these ‘things’ that changes is probably the very first ‘thing’ that a hibernate application developer needs to do: obtain a SessionFactory.

Without further ado, what follows is the shiny new way of doing this:

package main;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	private static SessionFactory configureSessionFactory()
			throws HibernateException {
		Configuration configuration = new Configuration().configure();
		StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
		return configuration.buildSessionFactory(;

	public static SessionFactory getSessionFactory() {
		return configureSessionFactory();

My thanks go to Krishna Srinivasan over at JavaBeat’s Java Dev Zone who provided the correct mechanism, even while the mighty participants at StackOverflow couldn’t agree as to how this should be done.

Tags: , ,

C, Java Enterprise Edition, JEE, J2EE, JBoss, Application Server, Glassfish, JavaServer Pages, JSP, Tag Libraries, Servlets, Enterprise Java Beans, EJB, Java Messaging Service JMS, BEA Weblogic, JBoss, Application Servers, Spring Framework, Groovy, Grails, Griffon, GPars, GAnt, Spock, Gradle, Seam, Open Source, Service Oriented Architectures, SOA, Java 2 Standard Edition, J2SE, Eclipse, Intellij, Oracle Service Bus, OSB