Spring OpenSessionInViewInterceptor不起作用 [英] Spring OpenSessionInViewInterceptor doesn't work

查看:90
本文介绍了Spring OpenSessionInViewInterceptor不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



    $ $在hibernate和延迟加载时出现了着名的问题。 b $ b
  • 使方法成为事务性的(这并不总是可以理解的)

  • 使用OpenSessionInViewInterceptor。



后者更可取IMO。无论如何,我不知道这个拦截器是否触发(事实上,我得到了相同的延迟加载异常,没有任何变化):

  org.hibernate.LazyInitializationException:无法延迟初始化角色集合:it.jsoftware.jacciseweb.beans.Listino.prodotti,没有会话或会话已关闭

我使用简单的基于注解的url映射,所以阅读Spring 3的文档,我在servlet-context.xml中使用它:

 < bean id =handlerMapping
class =org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping>
<! - < property name =ordervalue =2/> - >
< property name =interceptors>
< list>
< ref bean =openSessionInViewInterceptorInst/>
< / list>
< / property>
< / bean>

哪个应该可以做到。但它不起作用,我得到了例外。我如何确保拦截器正在发射?如何解决这个问题?

解决方案

您是否使用@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


I had the (in)famous problem with hibernate and lazy loading when views are rendered.... As many say, the only two solutions are:

  • Make the method transactional (and this is not always desiderable)
  • Use OpenSessionInViewInterceptor.

The latter is preferable IMO. Anyway I'm not sure if this interceptor is firing at all (in fact I get the same Lazy loading exception and nothing changes):

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: it.jsoftware.jacciseweb.beans.Listino.prodotti, no session or session was closed

I'm using simple annotation based url mappings, so reading the docs for Spring 3, I'm using this in my 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>

Which should make the trick. But it is not working and I get the exception. How do I make sure my interceptor is firing? How do I solve this?

解决方案

Are you using the @RequestMapping annotation? If I remember right there was an issue with putting the interceptor on the url bean. With Spring 3.0 you can define the interceptor like this:

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

assuming that sessionFactory is a reference to your SessionFactory bean.

You will also need to include the mvc namespace.

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"

这篇关于Spring OpenSessionInViewInterceptor不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆