liuhuiyu2004 2016-10-03 06:48 采纳率: 0%
浏览 1054

spring+hibernate 懒加载问题

错误信息

 严重: 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>
  • 写回答

2条回答 默认 最新

  • dabocaiqq 2016-10-03 07:01
    关注
    评论

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样