gpt4 book ai didi

java - 已弃用 Spring AnnotationConfiguration

转载 作者:行者123 更新时间:2023-11-30 02:41:58 26 4
gpt4 key购买 nike

我有以下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
谢谢

最佳答案

javadoc 说:

All functionality has been moved to 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);

关于java - 已弃用 Spring AnnotationConfiguration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41411697/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com