在配置类中重写WebMvcConfigurer#addInterceptors(InterceptorRegistry):
@Overridepublic void addInterceptors(InterceptorRegistry registry) { OpenSessionInViewInterceptor openSessionInViewInterceptor = new OpenSessionInViewInterceptor(); openSessionInViewInterceptor.setSessionFactory(getSessionFactory().getObject()); registry.addWebRequestInterceptor(openSessionInViewInterceptor).addPathPatterns("/**");}还要添加
@EnableWebMvcconfig类。
针对OP的评论:
我不确定为什么它不起作用。在我看来一切都很好。还有另一种方法可以实现此目的:
设置
hibernate.enable_lazy_load_no_trans属性
true。
见
23.9.1。 有关更多信息,请参见《 Hibernate用户指南》中的“
获取属性
”。
但是,如指南中所述,这 不是 一个很好的选择:
尽管启用此配置可以
LazyInitializationException取消,但最好使用获取计划,该计划可确保在关闭会话之前正确初始化所有属性。In reality, you shouldn’t probably enable this setting anyway.



