问题描述
我试图用spring 3.2和hibernate 4创建> ServiceRegistry 接口与服务的概念有关(这是Hibernate 4的新概念) 。服务是为Hibernate提供各种功能的类,为此用户可以插入其他实现。有关详细信息,请参见。
您在Hibernate的 Configuration 中不推荐使用方法 buildSessionFactory()类支持方法 buildSessionFactory(ServiceRegistry serviceRegistry)。在一个纯粹的Hibernate环境中(没有Spring),假设你将以这种方式初始化 ServiceRegistry 的实例:
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
private static SessionFactory configureSessionFactory()throws HibernateException {
Configuration configuration = new Configuration();
configuration.configure();
$ b $ serviceRegistry = new ServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
但现在不推荐使用的方法 buildSessionFactory()也为您执行 ServiceRegistry 的初始化。
Spring's LocalSessionFactoryBuilder class只是Hibernate的 Configuration 类的扩展。但由于Spring的所有特定工作都是在overriden方法 LocalSessionFactoryBuilder.buildSessionFactory()中完成的,因此不能使用方法 buildSessionFactory(ServiceRegistry serviceRegistry) code>在Spring的环境中。没有什么可以使用 buildSessionFactory()来完成相同的工作。因此,让我们用 @SuppressWarnings(deprecation)注释 AppConfig 中的方法,耐心等待Spring提供更好的与Hibernate 4集成。
I'm trying to create sessionFactory bean using spring 3.2 and hibernate 4. I used the following code for that. But the problem is buildSessionFactory() is deprecated and the buildSessionFactory(ServiceRegistry serviceRegistry) is suggested to use instead in javadoc. However, I'm not being able to understand what is ServiceRegistry and how to use buildSessionFactory(ServiceRegistry serviceRegistry).
@Configuration public class AppConfig { ... @Bean public SessionFactory sessionFactory() { return new LocalSessionFactoryBuilder(dataSource()) .scanPackages("com.mypackages") .addProperties(hibernateProperties()) } }
ServiceRegistry interface is related to concept of services (that is new for Hibernate 4). Services are classes that provide Hibernate with various functionality and for which user can plug in alternate implementations. See this wiki page for details.
You are right that method buildSessionFactory() is deprecated in Hibernate's Configuration class in favor of method buildSessionFactory(ServiceRegistry serviceRegistry). In a pure Hibernate's environment (without Spring) it is supposed that you will initialize instance of ServiceRegistry in such a way:
private static SessionFactory sessionFactory; private static ServiceRegistry serviceRegistry; private static SessionFactory configureSessionFactory() throws HibernateException { Configuration configuration = new Configuration(); configuration.configure(); serviceRegistry = new ServiceRegistryBuilder() .applySettings(configuration.getProperties()) .buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); return sessionFactory; }
But by now the deprecated method buildSessionFactory() also does the same initialization of ServiceRegistry for you.
Spring's LocalSessionFactoryBuilder class is just the extension of Hibernate's Configuration class. But since all the specific work of Spring is done in overriden method LocalSessionFactoryBuilder.buildSessionFactory() you can't use method buildSessionFactory(ServiceRegistry serviceRegistry) in Spring's environment. Nothing much 'cause it's ok to use buildSessionFactory() that does exactly the same work. So let's just annotate the method in AppConfig with @SuppressWarnings("deprecation") and patiently wait for Spring to provide better integration with Hibernate 4.
这篇关于用Spring,Hibernate和LocalSessionFactoryBuilder配置sessionFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!