我在渲染 View 时遇到了(臭名昭著的) hibernate 和延迟加载问题......正如许多人所说,唯一的两个解决方案是:

  • 使方法具有事务性(这并不总是可取的)
  • 使用 OpenSessionInViewInterceptor。

  • 后者是 IMO 的首选。无论如何,我不确定这个拦截器是否正在触发(实际上我得到了相同的延迟加载异常并且没有任何变化):
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: it.jsoftware.jacciseweb.beans.Listino.prodotti, no session or session was closed 
    

    我正在使用基于简单注释的 url 映射,所以阅读 Spring 3 的文档,我在我的 servlet-context.xml 中使用它:
    <bean id="handlerMapping" 
            class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> 
            <!-- <property name="order" value="2" /> --> 
            <property name="interceptors"> 
                <list> 
                    <ref bean="openSessionInViewInterceptorInst" /> 
                </list> 
            </property> 
        </bean> 
    

    这应该是诀窍。但它不起作用,我得到了异常(exception)。我如何确保我的拦截器正在发射?我该如何解决这个问题?

    请您参考如下方法:

    你在使用 @RequestMapping 注释吗?如果我没记错的话,将拦截器放在 url bean 上会出现问题。使用 Spring 3.0,您可以像这样定义拦截器:

    <mvc:interceptors> 
        <bean class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor"> 
            <property name="sessionFactory"> 
                <ref local="sessionFactory" /> 
            </property> 
        </bean> 
    </mvc:interceptors> 
    

    假设 sessionFactory 是对 SessionFactory bean 的引用。

    您还需要包含 mvc 命名空间。
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" 
    


    评论关闭
    IT干货网

    微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!