Method buildSessionFactory type Configuration deprecated

Method buildSessionFactory type Configuration deprecated

The method buildSessionFactory() from the type Configuration is deprecated

With new version of Hibernate 4.0, 4.1, 4.2, 4.3 buildSessionFactory() is deprecated so please use below sample Utility calss to get SessionFactory:

package com.javahonk.util;

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 = buildSessionFactory();
    private static ServiceRegistry serviceRegistry;

    private static SessionFactory buildSessionFactory() {
        try {
            
            Configuration configuration = new Configuration();
            configuration.configure();
            //set path of config file explicitly
            //configuration.configure("hibernate/hibernate.cfg.xml");
            serviceRegistry = new ServiceRegistryBuilder()
            .applySettings(configuration.getProperties()).build();
            sessionFactory = configuration
                    .buildSessionFactory(serviceRegistry);
            return sessionFactory;
            
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

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

One thought on “Method buildSessionFactory type Configuration deprecated

Leave a Reply

Your email address will not be published. Required fields are marked *