Hibernate4 buildSessionFactory过时解决方案HibernateUtil

来源:互联网 发布:sql培训班 编辑:程序博客网 时间:2024/05/21 11:09

hibernate 4    buildSessionFactory  已经过时 ,官方给出这样的写法

 

package com.bbs.tool;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.hibernate.service.ServiceRegistryBuilder; public class HibernateUtil {    private static final SessionFactory sessionFactory = buildSessionFactory();    private static SessionFactory buildSessionFactory() {        try {            // Create the SessionFactory from hibernate.cfg.xml         Configuration cfg = new Configuration();               cfg.configure();                       ServiceRegistry  sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();                        return cfg.buildSessionFactory(sr);          }        catch (Throwable ex) {            // Make sure you log the exception, as it might be swallowed            System.err.println("Initial SessionFactory creation failed." + ex);            throw new ExceptionInInitializerError(ex);        }    }    public static SessionFactory getSessionFactory() {        return sessionFactory;    }}