Hibernate4.3.8 buildSessionFactory过时解决方案HibernateUtil

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {
 
	private static SessionFactory sf=null;
	static{
		//1.创建Configuration,该对象用于读取hibernate.cfg.xml,并完成初始化
		Configuration cfg=new Configuration();
		cfg.configure();
		//2.创建SessoinFactory[这是一个会话工厂,是一个重量级的对象
		ServiceRegistry  sr = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();           
		sf = cfg.buildSessionFactory(sr);  
	}
	public static Session getSession(){
		
		return sf.openSession();
	}
}

阅读更多

没有更多推荐了,返回首页