gpt4 book ai didi

hibernate - Spring OpenSessionInViewInterceptor 不起作用

转载 作者:行者123 更新时间:2023-12-04 16:28:01 27 4
gpt4 key购买 nike

我在渲染 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"

    关于hibernate - Spring OpenSessionInViewInterceptor 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4992772/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com