您的位置:首页 > 产品设计 > UI/UE

Hibernate4.3.8 buildSessionFactory过时解决方案HibernateUtil

2015-01-15 13:54 417 查看
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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: