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;
}
}