错误信息
严重: Servlet.service() for servlet [springMvc] in context with path [/zgly4] threw exception [Request processing failed; nested exception is org.hibernate.LazyInitializationException: could not initialize proxy - no Session] with root cause
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
引发异常的代码
@RequestMapping(PAGE_TEST)
public String test(HttpServletRequest request, HttpServletResponse response, Model model){
System.out.println(ROOT+PAGE_TEST);
TbUdCemeteryService cemeteryService = ServiceFactory.produceTbUdCemeteryService();
List<TbUdCemetery> list=cemeteryService.findAll();
for(TbUdCemetery cemetery : list) {
String s=cemetery.getTbBsUser().getBuName();
}
model.addAttribute("test",list);
System.out.println(ROOT+PAGE_TEST+" OVER");
return WebAddress.MAIN_TEST;
}
其中报错的代码是
for(TbUdCemetery cemetery : list) {
String s=cemetery.getTbBsUser().getBuName();
}
这里是实现懒加载的地方
我已经加载了懒加载的Session的设定
在配置文件中加载了
<bean name="openSessionInViewInterceptor" class="org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="openSessionInViewInterceptor" />
</list>
</property>
<property name="mappings">
<props>
<prop key="/te*">homeController</prop>
</props>
</property>
</bean>