close

HibernateUtil For 5.x

---------------------------------------------------------------

import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
 
public class HibernateUtil
{
    private static SessionFactory sessionFactory = createSessionFactory();
 
    private static SessionFactory createSessionFactory()
    {
        try
        {
            StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
            return new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
        } catch (Exception e)
        {
            e.printStackTrace();
            throw new ExceptionInInitializerError(e);
        }
    }
 
    public static SessionFactory getSessionFactory()
    {
        return sessionFactory;
    }
 
    public static void closeSessionFactory()
    {
        if (sessionFactory != null)
        {
            sessionFactory.close();
        }
    }
}

---------------------------------------------------------------

 

arrow
arrow
    全站熱搜

    PhilChen 發表在 痞客邦 留言(0) 人氣()