当我将Hibernate版本从3.6.8更新到4.0.0时,buildSessionFactory()在此行中收到有关过时方法的警告:
buildSessionFactory()
private static final SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Javadoc建议使用另一种方法
buildSessionFactory(ServiceRegistry serviceRegistry)
但是在文档中我发现不推荐使用的变体
是的,已弃用。将你的SessionFactory替换为以下内容:
在Hibernate 4.0、4.1、4.2中
private static SessionFactory sessionFactory; private static ServiceRegistry serviceRegistry; public static SessionFactory createSessionFactory() { Configuration configuration = new Configuration(); configuration.configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings( configuration.getProperties()). buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); return sessionFactory; }
更新:
在Hibernate 4.3中,不推荐使用 ServiceRegistryBuilder 。请改用以下内容。
serviceRegistry = new StandardServiceRegistryBuilder().applySettings( configuration.getProperties()).build();