首页 > 代码库 > HibernateUtil

HibernateUtil

package util;


import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;


public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {
        try {//配置文件方法时:
         //sessionFactory = new Configuration().configure().buildSessionFactory();
         //注解方法时:
         sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);//初始化失败,输出异常
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}

HibernateUtil