Hibernate 4 Util SessionFactory example

import org.hibernate.HibernateException;
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;
    private static ServiceRegistry serviceRegistry;

//          Configuration configuration = new Configuration();
            Configuration configuration = new Configuration().configure();

            serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        catch (HibernateException he)
            System.err.println("Error creating Session: " + he);
            throw new ExceptionInInitializerError(he);

    public static SessionFactory getSessionFactory()
        return sessionFactory;

Credits: http://stackoverflow.com/questions/8621906/is-buildsessionfactory-deprecated-in-hibernate-4


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s