Customize sessionFactory with Spring, Hibernate, and LocalSessionFactoryBuilder

I am trying to create a sessionFactory bean using spring 3.2 and hibernate 4. For this, I used the following code. But the buildSessionFactory() problem is outdated, and buildSessionFactory(ServiceRegistry serviceRegistry) suggested to be used instead in javadoc. However, I cannot understand what ServiceRegistry is and how to use buildSessionFactory(ServiceRegistry serviceRegistry) .

 @Configuration public class AppConfig { ... @Bean public SessionFactory sessionFactory() { return new LocalSessionFactoryBuilder(dataSource()) .scanPackages("com.mypackages") .addProperties(hibernateProperties()) .buildSessionFactory(); } } 
+6
source share
3 answers
Interface

ServiceRegistry is related to the concept of services (which is new to Hibernate 4). Services are classes that provide various functions to Hibernate and for which the user can connect to alternative implementations. See this wiki page for more details.

You are correct that the buildSessionFactory() method is deprecated in the Hibernate Configuration class in favor of the buildSessionFactory(ServiceRegistry serviceRegistry) method. In a pure Hibernate environment (without Spring), it is assumed that you initialize the ServiceRegistry instance ServiceRegistry this:

 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 now the deprecated buildSessionFactory() method also does the same ServiceRegistry initialization for you.

The Spring LocalSessionFactoryBuilder class is just an extension of the Hibernate Configuration class. But since all the specific Spring work is done in the redefined LocalSessionFactoryBuilder.buildSessionFactory() method, you cannot use the buildSessionFactory(ServiceRegistry serviceRegistry) method in the Spring environment. Nothing special, because it's ok to use buildSessionFactory() , which does exactly the same job. So let's just annotate the method in AppConfig with @SuppressWarnings("deprecation") and wait patiently for Spring to provide better integration with Hibernate 4.

+9
source

You can also write code without binding:

 LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource()); builder.scanPackages("com.mypackages"); builder.addProperties(hibernateProperties()); return builder.buildSessionFactory(); 

A bit more verbose, but not as smelly as @SuppressWarnings ("deprecation")

0
source

The answer posed by Artem Shafranov is not entirely correct, and you may encounter a very confusing problem, just like me.

If you use

 hibernate.hbm2ddl.auto 

your application will not start because the connection pool in hbm2dll will be set to UserSuppliedConnectionProviderImpl (basically a good way to say: null). You will see this exception:

 org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource] 

The reason for this is due to ServiceRegistry, which is used by hbm2dll, but which does not play well with Spring. Using many of the proposed programming methods for setting up a session, it will not yet have the correct link when executing hbm2dll.

The only way that worked for me is

 @Inject DataSource datasource; @Bean @SuppressWarnings("deprecation") public SessionFactory sessionFactory() throws IOException{ LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean(); sessionFactoryBean.setPackagesToScan("nl.your.model"); sessionFactoryBean.setHibernateProperties(hibernateProperties()); sessionFactoryBean.setDataSource(datasource); sessionFactoryBean.afterPropertiesSet(); return sessionFactoryBean.getObject(); } 

Failure to use LocalSessionFactoryBuilder. Using StandardServiceRegistryBuilder, by the way, also failed.

Really confusing issue.

0
source

All Articles