不推荐使用Spring AnnotationConfiguration

时间:2016-12-31 21:00:41

标签: java hibernate netbeans

我有以下java代码:



package modelo.util;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;

public class HibernateUtil {

    private static final SessionFactory sessionFactory;
    
    static {
        try {
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
    
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}




以下行是否弃用了上述代码:

import org.hibernate.cfg.AnnotationConfiguration;
...= new AnnotationConfiguration().configure().buildSessionFactory();

我正在使用hibernate 4.3.1Netbeans
感谢

1 个答案:

答案 0 :(得分:2)

javadoc说:

  

所有功能都已移至Configuration

只需将org.hibernate.cfg.AnnotationConfiguration替换为org.hibernate.cfg.Configuration

Configuration.buildSessionFactory已弃用,取而代之的是采用ServiceRegistry的方法。您必须使用ServiceRegistryBuilder配置一个。

Configuration configuration = new Configuration();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
builder.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = builder.build();
sessionFactory = configuration.configure().buildSessionFactory(serviceRegistry);