Package dao

Source Code of dao.HibernateUtil

package dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

  private static SessionFactory sessionFactory;

  private static StandardServiceRegistry sr;
  private HibernateUtil(){

  }

  public static SessionFactory getSessionFactory() {
    if(sessionFactory == null)
      try{
        Configuration cfg = new Configuration().configure("hibernate.cfg.xml");  
        StandardServiceRegistryBuilder sb = new StandardServiceRegistryBuilder();
        sb.applySettings(cfg.getProperties());
        StandardServiceRegistry standardServiceRegistry = sb.build();            
        sessionFactory = cfg.buildSessionFactory(standardServiceRegistry);
        sr = standardServiceRegistry;
      } catch (Throwable ex) {
        System.err.println("Failed to create sessionFactory object." + ex);
        throw new ExceptionInInitializerError(ex);
      }

    return sessionFactory;
  }
 
  public static Session openSession(){
    return getSessionFactory().openSession();
  }


  public static StandardServiceRegistry getSR(){
    return sr;
  }
 
 
}
TOP

Related Classes of dao.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.