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.
source share