适用于 Hibernate 5 的 Spring Boot 1.5.x 的正确和完整配置



关于1.5.10.RELEASE版本的Spring Boot。 它在内部与版本5.0.12.FinalHibernate一起工作

目的是避免以下错误消息:

required a bean of type 'org.hibernate.SessionFactory' that could not be found

应应用HibernateJpaSessionFactoryBean类。它根据:

  • 需要找不到"org.hibernate.SessionFactory"类型的 bean

这里的情况是HibernateJpaSessionFactoryBean类是@Deprecated

根据HibernateJpaSessionFactoryBean javadoc建议的解决方案是使用EntityManagerFactory#unwrap

因此来自:

  • Spring Boot - 休眠会话工厂的句柄

手动必须声明以下内容:

@Bean
public SessionFactory sessionFactory(EntityManagerFactory emf) {
return emf.unwrap(SessionFactory.class);
}

警告是强制性的,包括以下内容application.properties文件(上面分享的帖子中未提及):

spring.jpa.properties.hibernate.current_session_context_class = 
org.springframework.orm.hibernate5.SpringSessionContext

否则显示:

org.springframework.orm.jpa.JpaSystemException:
No CurrentSessionContext configured!; nested exception is org.hibernate.HibernateException:
No CurrentSessionContext configured!

直到这里我的@Test类通过Hibernate失败,这些相同的@Test类通过Spring Framework传递到其他项目中,因此所有用于Hibernate的基础设施都被正式声明。

因此,通过以下代码:

@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());
}
}

Spring Boot创建的所有beans都已打印出来,我已经确认了以下内容:

- PlatformTransactionManager 
- Class: org.springframework.orm.jpa.JpaTransactionManager

我期望HibernateTransactionManager而不是JpaTransactionManager.

我让@Test方法通过的独特方法是再次手动声明其他@Bean

@Bean
public PlatformTransactionManager transactionManager(SessionFactory sessionFactory){
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory);
return transactionManager;
}

因此:

  • Hibernate 5Spring Boot 1.5.x的正确和完整配置是什么?

观察:如果一切都通过application.properties文件完全配置,那就更好了(目的避免手动声明任何@Bean)

总结如何,集成Spring Boot的独特方式为普通Hibernate(考虑将整个项目通过Spring Framework迁移到Spring Boot同时使用Hibernate的场景) 是通过以下内容:

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

加上这两个强制性@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;
}

如果没有这两个@Beans我已经报告了所有两个错误。

因此,目标是通过application.properties配置Hibernate

这是一个建议(Spring Boot 1.5.10.RELEASE)

文件应用程序属性

spring.datasource.url=jdbc:mysql://localhost:3306/vy
spring.datasource.username=root
spring.datasource.password=123456
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

文件BeanConfig.java

package com.example;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.persistence.EntityManagerFactory;
@Configuration
public class BeanConfig {
@Autowired
private EntityManagerFactory entityManagerFactory;
@Bean
public SessionFactory getSessionFactory() {
if (entityManagerFactory.unwrap(SessionFactory.class) == null) {
throw new NullPointerException("factory is not a hibernate factory");
}
return entityManagerFactory.unwrap(SessionFactory.class);
}
}

文件用户道.java

package com.example.dao;
import com.example.model.UserDetails;
import java.util.List;
public interface UserDao {
List<UserDetails> getUserDetails();
}

文件用户DaoImpl.java其中使用会话工厂

package com.example.dao.impl;
import com.example.dao.UserDao;
import com.example.model.UserDetails;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class UserDaoImpl implements UserDao {
@Autowired
private SessionFactory sessionFactory;
public List<UserDetails> getUserDetails() {
Criteria criteria = sessionFactory.openSession().createCriteria(UserDetails.class);
return criteria.list();
}
}

目标是仅通过应用程序属性配置休眠

--> 使用休眠会话的 JPA 实现是可以的。Spring Data JPA与Spring Boot紧密集成(至少我用SpringBoot 2.0.0.RC1检查过)。通过application.properties进行配置,在引擎盖下,Spring Boot是自动配置的。

Hibernate ORM的全部功能是不行的。

最新更新