传统项目 升级到 spring boot 配置 org.springframework.orm.hibernate5.support.OpenSessionInViewFilter
OpenSessionInViewFilter (Spring Framework 5.2.9.RELEASE API)
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/orm/hibernate5/support/OpenSessionInViewFilter.html
OpenSessionInViewInterceptor (Spring Framework 5.2.9.RELEASE API)
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/orm/hibernate5/support/OpenSessionInViewInterceptor.html
org.springframework.orm.hibernate5.support (Spring Framework 5.2.9.RELEASE API)
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/orm/hibernate5/support/package-summary.html
OpenSessionInViewFilter (Spring Framework API 2.5)
https://docs.spring.io/spring-framework/docs/2.5.4/javadoc-api/org/springframework/orm/hibernate3/support/OpenSessionInViewFilter.html
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class,JdbcTemplateAutoConfiguration.class,
FreeMarkerAutoConfiguration.class,ValidationAutoConfiguration.class,ServerPropertiesAutoConfiguration.class,MailSenderAutoConfiguration.class})
public class CrmApplication
{
public static void main(String[] args) {
SpringApplication.run(CrmApplication.class, args);
//configureApplication(new SpringApplicationBuilder()).run(args);
}
// @Override
// protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// return configureApplication(builder);
// }
@Bean
public FilterRegistrationBean getHibernateOpenSessionInViewFilter() {
FilterRegistrationBean hibernateOpenSessionInViewFilterRegistrationBean = new FilterRegistrationBean();
hibernateOpenSessionInViewFilterRegistrationBean.setFilter(new OpenSessionInViewFilter());
hibernateOpenSessionInViewFilterRegistrationBean.setEnabled(true);
hibernateOpenSessionInViewFilterRegistrationBean.addUrlPatterns("*.do");
hibernateOpenSessionInViewFilterRegistrationBean.addUrlPatterns("/rest/*");
hibernateOpenSessionInViewFilterRegistrationBean.setInitParameters(new HashMap<String, String>() {
private static final long serialVersionUID = -4377783733248874717L;
{
//put("sessionFactoryBeanName", "sessionFactory");
put("singleSession", "true");
//put("flushMode", "AUTO");
}
});
hibernateOpenSessionInViewFilterRegistrationBean.setOrder(1);
return hibernateOpenSessionInViewFilterRegistrationBean;
}
参考了如下代码:
hibernate - Using Spring Boot and Data JPA, getting LazyInitializationException despite the OpenSesionInViewFilter - Stack Overflow
https://stackoverflow.com/questions/36979281/using-spring-boot-and-data-jpa-getting-lazyinitializationexception-despite-the