今天在SpringBoot中Hibernate进行数据save时,不报错,也不进行数据保存,问题分析因为了在支持Hibernate的Lazy init,初步怀疑是hibernate的session没有关闭,在openSessionInViewFilter中提定FlushMode为AUTO,或者Commit
/***
* 增加Hibernate的Lazy init 支持
* @return
*/
@Bean
public FilterRegistrationBean openSessionInViewFilterRegistrationBean() {
FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
OpenSessionInViewFilter filter = new OpenSessionInViewFilter();
filterRegistration.setFilter(filter);
filterRegistration.addUrlPatterns("/*");
filter.setFlushMode(FlushMode.AUTO);
filter.setSingleSession(true);
filter.setSessionFactoryBeanName("sessionFactory");
filterRegistration.setAsyncSupported(true);
filterRegistration.setOrder(1);
return filterRegistration;
}
再次测试问题依旧,只是部分表的操作save失败,然后在失败的地方,调用dao.getHibernateTemplate().flush()手动刷新即可
原因就是hibernatesession没有关闭导致,所以手动刷新后问题解决