如何使OpenSessionInViewInterceptor在Spring MVC中工作

显示名称 :

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

以下是我已经拥有的代码以及错误的出处。

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

<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来实现它。

马哈茂德峰:

在配置类中重写WebMvcConfigurer#addInterceptors(InterceptorRegistry)

@Override
public void addInterceptors(InterceptorRegistry registry) {
    OpenSessionInViewInterceptor openSessionInViewInterceptor = new OpenSessionInViewInterceptor();
    openSessionInViewInterceptor.setSessionFactory(getSessionFactory().getObject());

    registry.addWebRequestInterceptor(openSessionInViewInterceptor).addPathPatterns("/**");
}

还要添加@EnableWebMvcconfig类。

针对OP的评论:

我不确定为什么它不起作用。在我看来一切都很好。还有另一种方法可以实现此目的:

设置hibernate.enable_lazy_load_no_trans属性true

23.9.1。有关更多信息,请参见《 Hibernate用户指南》中的“ 获取属性

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

尽管启用此配置可​​以LazyInitializationException取消,但最好使用获取计划,该计划可确保在关闭会话之前正确初始化所有属性。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

@SessionAttribute在Spring MVC中如何工作?

基于Java的配置在Spring MVC中如何工作

转换器如何在Spring MVC中工作?

Session在MVC中实际如何工作?

如何让 Kendo DatePicker 在 MVC Partial 中工作?

Spring MVC:“视图”如何工作?

Spring MVC:Spring Bean如何工作?

Spring MVC VersionResourceResolver / ContentVersionStrategy在JSP中无法正常工作

ASP.NET MVC中的ViewBag如何工作

前端路由器如何在MVC框架中工作?

身份如何在ASP .NET MVC 5中工作

我如何使scrapysharp在MVC Web应用程序中工作?

使用Spring MVC Frame工作,如何在bingmap中迭代列表并创建图钉?

在Spring Web MVC Cycle中,formbacking对象和referencedata对象如何工作?

Bean的会话范围在Spring MVC应用程序中如何工作?

Spring MVC Java中的视图引擎是什么以及它如何工作?

请求参数绑定和类型转换在spring-mvc中如何工作?

无法使 Spring MVC 工作

Spring中ApplicationContextAware如何工作?

Tiles中的图像/ CSS /样式和包含的页面中的Spring MVC无法正常工作

一些疑问与Spring MVC中的显式页面映射有关,究竟如何工作?

Spring Batch:以新的思路从Spring MVC控制器中开始工作

Spring MVC的自定义格式工作在测试,但在浏览器中失败

使用@Valid会引发异常,并且在基本的Spring 3.0 MVC程序中无法正常工作

Azure Web App Service日志记录中的Spring Boot MVC无法按预期工作

没有XML的Spring MVC中的双调度程序配置,URL映射无法按预期工作

Spring MVC控制器方法参数如何工作?

豆子自动接线在Spring中如何工作?

“记住我”在Spring Security中如何工作?