About Spring Boot
for the 1.5.10.RELEASE
version.
It works internally around with Hibernate
for version 5.0.12.Final
With the purpose to avoid the following error message:
required a bean of type 'org.hibernate.SessionFactory' that could not be found
The HibernateJpaSessionFactoryBean
class should be applied. It according from:
The situation here is the HibernateJpaSessionFactoryBean
class is @Deprecated
.
The solution how is suggested according the HibernateJpaSessionFactoryBean javadoc, is work around with EntityManagerFactory#unwrap
Thus from:
manually must be declared the following:
@Bean
public SessionFactory sessionFactory(EntityManagerFactory emf) {
return emf.unwrap(SessionFactory.class);
}
Warning is mandatory include in the application.properties
file the following (it is not mentioned in the post shared above):
spring.jpa.properties.hibernate.current_session_context_class =
org.springframework.orm.hibernate5.SpringSessionContext
Otherwise appears:
org.springframework.orm.jpa.JpaSystemException:
No CurrentSessionContext configured!; nested exception is org.hibernate.HibernateException:
No CurrentSessionContext configured!
Until here my @Test
classes working through Hibernate
fails, these same @Test
classes pass in other project working through Spring Framework
, thus all the infrastructure for Hibernate
is declared manully.
Therefore through the following code:
@Test
public void contextLoads() {
String[] beanNames = applicationContext.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
logger.info("beanName: {}", beanName);
}
logger.info("PlatformTransactionManager");
if(transactionManager != null) {
logger.info("Class: {}", transactionManager.getClass().getName());
}
}
all the beans
created by Spring Boot
are printed and I have confirmed the following:
- PlatformTransactionManager
- Class: org.springframework.orm.jpa.JpaTransactionManager
I expected the HibernateTransactionManager
instead of JpaTransactionManager
.
My unique way to get the @Test
methods passing is declaring again manually other @Bean
:
@Bean
public PlatformTransactionManager transactionManager(SessionFactory sessionFactory){
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory);
return transactionManager;
}
Therefore:
- What is the correct and complete configuration for
Spring Boot 1.5.x
forHibernate 5
?
Observation: even better if all is completely configured through the application.properties
file (purpose avoid declare manually any @Bean
)
How a summary, the unique way to integrate Spring Boot
for plain Hibernate
(Consider the scenario to migrate a complete project working through Spring Framework
to Spring Boot
working both with Hibernate
)
is through the following:
spring.jpa.hibernate.ddl-auto = none
spring.jpa.properties.hibernate.cache.provider_class = org.hibernate.cache.NoCacheProvider
spring.jpa.properties.hibernate.current_session_context_class = org.springframework.orm.hibernate5.SpringSessionContext
spring.jpa.properties.hibernate.default_batch_fetch_size = 30
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.H2Dialect
spring.jpa.properties.hibernate.format_sql = true
spring.jpa.properties.hibernate.jdbc.batch_size = 30
spring.jpa.properties.hibernate.max_fetch_depth = 30
spring.jpa.properties.hibernate.order_updates = true;
spring.jpa.properties.hibernate.show_sql = false
spring.jpa.properties.hibernate.use_sql_comments = true
Plus these two mandatories @Beans
@Bean
public SessionFactory sessionFactory(EntityManagerFactory emf) {
return emf.unwrap(SessionFactory.class);
}
@Bean
public PlatformTransactionManager transactionManager(SessionFactory sessionFactory){
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory);
return transactionManager;
}
Without these two @Beans
I have all the two errors already reported.
Therefore the goal is configure Hibernate
just through application.properties