是否在hibernate 4中弃用bui​​ldSessionFactory()?

问题:

当我将Hibernate的版本从3.6.8.final更新为4.0.0.final时,我收到了关于此行中已弃用的方法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;
}

 更新:

休眠4.3中,ServiceRegistryBuilder是弃用。使用以下代替。

serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
            configuration.getProperties()).build();

 
 
Code问答: http://codewenda.com/topics/python/
Stackoverflow: Is buildSessionFactory() deprecated in hibernate 4?

*转载请注明本文链接以及stackoverflow的英文链接

发表评论

电子邮件地址不会被公开。 必填项已用*标注

+ 13 = 14