代码优化:SessionFactory过期项

来源:互联网 发布:2016淘宝热卖行业 编辑:程序博客网 时间:2024/06/09 22:10

在查找服务器启动报错的过程中在HibernateSessionFactory类中发现了这个过期项
这里写图片描述
提示deprecate,是不赞成这么写的

错误原因:查找一番,发现在Hibernate的版本升级过程中,已经有了更好创建sessionFactory的选择

public class HibernateSessionFactory {    private static Configuration cfg;    private static SessionFactory sessionFactory;    private static ServiceRegistry buildServiceRegistry;    static {        try {            cfg = new Configuration().configure();            buildServiceRegistry = new ServiceRegistryBuilder()                    .applySettings(cfg.getProperties())                    .buildServiceRegistry();            sessionFactory = cfg.buildSessionFactory(buildServiceRegistry);        } catch (HibernateException e) {            // TODO Auto-generated catch block            throw new RuntimeException("hibernate初始化失败",e);        }    }    public static Session getSession(){        //一个用户可产生一个不同的session        return sessionFactory.getCurrentSession();    }}

后来发现虽然那种过时的方法不影响运行,但是还是建议使用新版本的优化方法

原创粉丝点击