如何使OpenSessionInViewInterceptor在Spring MVC中工作 [英] How to make OpenSessionInViewInterceptor work in Spring mvc

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

问题描述

我正在尝试在Spring MVC中设置OpenSessionInViewInterceptor以修复:org.hibernate.LazyInitializationException:无法初始化代理-没有会话.

I'm trying to set up OpenSessionInViewInterceptor in spring mvc to fix: org.hibernate.LazyInitializationException: could not initialize proxy - no Session.

下面是我已经拥有的代码以及错误的来源.

Below is the code I already have and where the error comes from.

AppConfig.java

AppConfig.java

@Configuration
@PropertySource("classpath:db.properties")
@EnableTransactionManagement
@ComponentScans(value = { @ComponentScan("com.debugger.spring.web.tests"),  @ComponentScan("com.debugger.spring.web.service"), @ComponentScan("com.debugger.spring.web.dao"),
@ComponentScan("com.debugger.spring.web.controllers") })
public class AppConfig implements WebMvcConfigurer {

@Autowired
private Environment env;

@Bean
public LocalSessionFactoryBean getSessionFactory() {
    LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();

    Properties props = new Properties();

    // Setting JDBC properties
    ...

    // Setting Hibernate properties
    ...

    // Setting C3P0 properties
        ...

    return factoryBean;
}

@Bean
public OpenSessionInViewInterceptor openSessionInViewInterceptor() {
    OpenSessionInViewInterceptor openSessionInViewInterceptor = new OpenSessionInViewInterceptor();
    openSessionInViewInterceptor.setSessionFactory(getSessionFactory().getObject());
    return openSessionInViewInterceptor;
}
}

featured.jsp

featured.jsp

<c:choose>
                            <c:when
                                test='${article.user.isSubscribed() and article.user.subscription.type eq "silver" }'>
                                <a class="bold"
                                    href='${pageContext.request.contextPath}/u/${article.user.username}'><span
                                    class="silvername"> <c:out value="${article.user.name}"></c:out></span></a>
                            </c:when>
                            <c:when
                                test='${article.user.isSubscribed() and article.user.subscription.type eq "gold" }'>
                                <a class="bold"
                                    href='${pageContext.request.contextPath}/u/${article.user.username}'><span
                                    class="goldname"> <c:out value="${article.user.name}"></c:out></span></a>
                            </c:when>
                            <c:when
                                test='${article.user.isSubscribed() and article.user.subscription.type eq "premium" }'>
                                <a class="bold"
                                    href='${pageContext.request.contextPath}/u/${article.user.username}'><span
                                    class="premiumname"> <c:out
                                            value="${article.user.name}"></c:out></span></a>
                            </c:when>
                            <c:otherwise>
                                <a class="bold"
                                    href='${pageContext.request.contextPath}/u/${article.user.username}'><span>
                                        <c:out value="${article.user.name}"></c:out>
                                </span></a>
                            </c:otherwise>
                        </c:choose>

$ {article.user.isSubscribed()}最有可能引发错误,因为无法获取用户.我希望它可以不使用急切的获取程序来运行,并且我认为可以通过正确设置OpenSessionInViewInterceptor来实现它.

${article.user.isSubscribed()} throws the error most likely because user cannot be fetched. I want it to run without using eager fetch and I think I can acheive it by setting up OpenSessionInViewInterceptor correctly.

推荐答案

覆盖

还要在配置类上添加@EnableWebMvc.

针对OP的评论:

我不确定为什么它不起作用.对我来说,一切似乎都很好.还有另一种方法可以实现:

I am not sure why it is not working. Everything seems fine to me. There is another way you can achieve this:

设置hibernate.enable_lazy_load_no_trans属性true.

请参见 23.9 .1.有关更多信息,请参见《 Hibernate用户指南》中的获取属性" .

但这不是 指南中所述的一个很好的选择:

But this is not a very good option as stated in the guide:

尽管启用此配置可​​以使 LazyInitializationException走开,最好使用提取计划 保证所有属性在开始之前都已正确初始化 该会话已关闭.

Although enabling this configuration can make LazyInitializationException go away, it’s better to use a fetch plan that guarantees that all properties are properly initialized before the Session is closed.

In reality, you shouldn’t probably enable this setting anyway.

这篇关于如何使OpenSessionInViewInterceptor在Spring MVC中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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