当我将Hibernate版本从3.6.8更新到4.0.0时,在此行中收到有关过时方法buildSessionFactory()
的警告:
private static final SessionFactory sessionFactory =
new Configuration().configure().buildSessionFactory();
Javadoc建议使用另一种方法
buildSessionFactory(ServiceRegistry serviceRegistry)
但在documentation中,我发现不推荐使用的变体:(
您能帮我一点点的误会吗?
最佳答案
是的,已弃用。将您的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();