Hibernate 官方单例获取sessionFactoty

package com.newer;

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

/**
 * Hibernate官方提供的工具类
 */
public class HibernateUtil {
          private static final SessionFactory sessionFactory; //单态模式
          static{
                   try {
                            sessionFactory=new AnnotationConfiguration().
                                         configure("hibernate.cfg.xml").buildSessionFactory(); //加载配置

                   } catch (HibernateException e) {
                            System.err.println("加载配置错误"+e);
                            throw new ExceptionInInitializerError(e); //抛出异常
                   }
          }

          public static SessionFactory getSessionfactory() {
                   return sessionFactory;
          }
 
}

你可能感兴趣的:(Hibernate 官方单例获取sessionFactoty)