Package com.jcasey.util

Source Code of com.jcasey.util.HibernateUtil

package com.jcasey.util;

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 Configuration configuration;
    private static SessionFactory sessionFactory;

    private static SessionFactory buildSessionFactory() {
        try {
          // Create the SessionFactory from hibernate.cfg.xml
          if(configuration == null)
          {
            configuration = new Configuration();
            configuration.configure("hibernate.cfg.xml");
          }
          ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                  .applySettings(configuration.getProperties()).build();
          return configuration.buildSessionFactory(serviceRegistry);
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        if(sessionFactory == null)
        {
          sessionFactory = buildSessionFactory();
        }
      return sessionFactory;
    }

  public static Configuration getConfiguration() {
    return configuration;
  }

  public static void setConfiguration(Configuration configuration) {
    HibernateUtil.configuration = configuration;
  }

}
TOP

Related Classes of com.jcasey.util.HibernateUtil

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.