我有以下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.1
和Netbeans
。
感谢
答案 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);