懒加载问题
在进行OA系统中用户管理的相关部署,配置到服务器后发现访问报异常
异常情况如下:
org.apache.jasper.JasperException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.jxust.oa.domain.User.roles, no session or session was closed org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:556) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) javax.servlet.http.HttpServlet.service(HttpServlet.java:731) org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748) org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486) org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411) org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338) org.apache.struts2.dispatcher.ServletDispatcherResult.doExecute(ServletDispatcherResult.java:164) org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:191) com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:369) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:273) org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.jxust.oa.domain.User.roles, no session or session was closed org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383) org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375) org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368) org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111) org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:186) org.apache.struts2.util.MakeIterator.convert(MakeIterator.java:82) org.apache.struts2.components.IteratorComponent.start(IteratorComponent.java:269) org.apache.struts2.views.jsp.ComponentTagSupport.doStartTag(ComponentTagSupport.java:54) org.apache.jsp.WEB_002dINF.jsp.userAction.list_jsp._jspx_meth_s_005fiterator_0
通过异常的描述和在网上查资料后发现这个是懒加载异常,就是在查询时没有加载关联表的对象,你读取这个关联对象的时候,hibernate的session已经关闭,所以无法获取对象。
解决办法是在web.xml中配置Spring的
<!-- 配置Spring的OpenSessionInViewFilter,以解决懒加载异常的问题 --> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping>
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!--在启动Web容器时,自动装配Spring applicationContext.xml的配置信息。自动的将其中的bean初始化 --> <!-- 容器初始化在这步完成 --> <!-- 配置spring的监听器用于初始化ApplicationContext对象 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <!-- 配置Spring的OpenSessionInViewFilter,以解决懒加载异常的问题 --> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <!-- 配置struts2的主过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
问题解决,成功得到预期效果
相关推荐
Kafka 2020-09-18
Wepe0 2020-10-30
杜倩 2020-10-29
windle 2020-10-29
minerd 2020-10-28
mengzuchao 2020-10-22
Junzizhiai 2020-10-10
bxqybxqy 2020-09-30
风之沙城 2020-09-24
kingszelda 2020-09-22
大唐帝国前营 2020-08-18
yixu0 2020-08-17
TangCuYu 2020-08-15
xiaoboliu00 2020-08-15
songshijiazuaa 2020-08-15
xclxcl 2020-08-03
zmzmmf 2020-08-03
newfarhui 2020-08-03
likesyour 2020-08-01