HibernateUtil 获取session工具类

package com.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
/*用于创建SessionFactory的工具类,官方写法*/
public class HibernateUtil {
	
	private static final SessionFactory sessionFactory=bulidSessionFactory();
	
	private static SessionFactory bulidSessionFactory(){
		try {
			Configuration cfg=new Configuration();
			cfg.configure();
			return  cfg.buildSessionFactory(
					new StandardServiceRegistryBuilder().
					applySettings(cfg.getProperties()).build() );
			
		} catch (Throwable ex) {
			throw new ExceptionInInitializerError(ex);
		}
	}
	
	public static SessionFactory getSessionFactory(){
		return sessionFactory;
	}

}

 

你可能感兴趣的:(Hibernate)