hibernate4.0以后创建seesionFactory
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 final SessionFactory sessionFactory = buildSessionFactory();
//在4.0以后的版本,此方法已经过时
private static SessionFactory buildSessionFactory1() {
return new Configuration().configure().buildSessionFactory();
}
private static SessionFactory buildSessionFactory() {
try {
Configuration configure= new Configuration().configure();
ServiceRegistry serviceRegistry=
new StandardServiceRegistryBuilder()
.applySettings(configure.getProperties())
.build();
return configure.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
private static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static org.hibernate.Session getSession() {
return sessionFactory.openSession();
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。