package com.capgemini.reports.database;

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

/**
 * Utility class for Hibernate accesses.
 **/
public final class HibernateUtil {
  private static final SessionFactory SESSION_FACTORY = buildSessionFactory();

  private HibernateUtil() {
    // Nothing to do...
  }

  /**
   * Creating a session factory, if not already loaded.
   **/
  private static SessionFactory buildSessionFactory() {
    Configuration configuration = new Configuration();
    configuration.configure();
    return configuration.buildSessionFactory(new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry());
  }

  /**
   * Getter for the factory instance.
   *
   * @return Factory instance
   **/
  public static SessionFactory getSessionFactory() {
    return SESSION_FACTORY;
  }

}
