package com.dev.hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; public class HibernateUtils { private static SessionFactory factory; static { Configuration cfg = new Configuration().configure(); ServiceRegistry sr = new ServiceRegistryBuilder().applySettings( cfg.getProperties()).buildServiceRegistry(); factory = cfg.buildSessionFactory(sr); } public static SessionFactory getSessionFactory() { return factory; } public static Session getSession() { return factory.openSession(); } public static void closeSession(Session session) { if (session != null) { if (session.isOpen()) { session.close(); } } } }