SSH2结构下openSessionInView无效,求指教!查了一天了,谢谢大家了...

royaki 2010-04-08 06:08:40
各位高手,内容很简单,请务必帮忙看看啊...

事务传播特性:
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* stu.service.*.*(..))"/>
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
</aop:config>


web.xml中这么配置:
<!-- Spring声明 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext-*.xml</param-value>
</context-param>
<!-- spring上下文监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- OpenSessionInView -->
<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>/*</url-pattern>
</filter-mapping>

<!-- struts2.1 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- ActionContextCleanUp -->
<filter>
<filter-name>ActionContextCleanUp</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>ActionContextCleanUp</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


<!-- 字符编码Filter -->
<filter>
<filter-name>Spring character encoding filter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Spring character encoding filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


struts.xml:
<constant name="struts.objectFactory" value="spring"/>



在前台页面调用
${user.newsSorts}时报错:

10-04-08 18:01:33 ERROR LazyInitializationException:19 - failed to lazily initialize a collection of role: stu.model.User.newsSorts, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: stu.model.User.newsSorts, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:249)
at org.apache.taglibs.standard.tag.common.core.ForEachSupport.toForEachIterator(ForEachSupport.java:348)
at org.apache.taglibs.standard.tag.common.core.ForEachSupport.supportedTypeForEachIterator(ForEachSupport.java:224)
at org.apache.taglibs.standard.tag.common.core.ForEachSupport.prepare(ForEachSupport.java:155)
at javax.servlet.jsp.jstl.core.LoopTagSupport.doStartTag(LoopTagSupport.java:256)
at org.apache.jsp.login.left_jsp._jspService(left_jsp.java:221)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:102)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
2010-4-8 18:01:33 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: stu.model.User.newsSorts, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:249)
at


我测试了一下,在action中newsSort可以取出来并发送sql语句,但是到了前台貌似我配置的openSessionInView就失效了!

望高手们不吝指教!
Q : 83534146
...全文
524 点赞 收藏 17
写回复
17 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
kongxue1220 2011-05-13
请问结果解决了嘛?我也用的注解配置的关系,也报错,求救!
回复
leishf 2010-04-12
我也遇到了同样的问题。郁闷啊
回复
wg243964183 2010-04-12
这个异常信息是延迟初始化异常。简单来说呢你用的那个对象应该用Hibernate.initialize(Object)这个方法来显示的初始化一下你用到的对象OK。用到那个Object就添那个。
回复
perior 2010-04-11
[Quote=引用 9 楼 royaki 的回复:]
我使用的注解
@OneToMany(mappedBy="user",fetch=FetchType.LAZY)
private List<NewsSort> newsSorts;
[/Quote]
这个引用是干什么的?
回复
存在666 2010-04-10
lazy设置为false就可以了!
回复
royaki 2010-04-09
[Quote=引用 8 楼 yxfei666 的回复:]

试试把 hibernate 映射文件中的 lazy设置为true
[/Quote]
我使用的注解
@OneToMany(mappedBy="user",fetch=FetchType.LAZY)
private List<NewsSort> newsSorts;
回复
perior 2010-04-09
试试把 hibernate 映射文件中的 lazy设置为true
回复
dongguang1082 2010-04-09
UP,shit,,,鼎
回复
royaki 2010-04-09
后台没有显示的关闭session

//查找用户
public User findUser(String username,String password) throws Exception
{
String hql="from User user where user.username=? and user.password=?";
return (User)this.getHibernateTemplate().find(hql,new Object[]{username,password}).get(0);
}
这样进行的操作。

非常简单的操作
回复
chcbz 2010-04-09
我觉得楼上的说法比较合理,不过我不建议用openSessionInView,因为用openSessionInView会令事务很难管理.
回复
zidasine 2010-04-09
基本为 过滤器顺序问题 我blog有OpenSessionInViewFilter失效的文章

<!-- struts2.1 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- ActionContextCleanUp -->
<filter>
<filter-name>ActionContextCleanUp</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>ActionContextCleanUp</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
将这2个换下位置


回复
wzy728 2010-04-09
既然是SSH 应该就可以用org.springframework.orm.hibernate3.support.OpenSessionInViewFilter过滤器了吧 这样的效率高些

我就是看这个http://blog.csdn.net/Siobhan/archive/2009/08/04/4407686.aspx 解决的问题
回复
markey_gu 2010-04-08
ActionContextCleanUp 放到 struts2 上面
回复
perior 2010-04-08
failed to lazily initialize a collection of role: stu.model.User.newsSorts, no session or session was closed
hibernate 使用不恰当,hibernate部分代码看看,否则无法查出原因
回复
BigBird2012 2010-04-08
[Quote=引用 2 楼 dickli1986 的回复:]
你的Hibernate使用的延迟加载,当加载类的时候,session已经关闭了,所以无法加载那个类,在加载之前不要关闭session
[/Quote]
估计你在程序中用“session.close()”方法显式的关闭了session,一般来说,session在Spring管理的情况下,不必显式调用session的close()方法关闭session
好好检查一下你的程序代码
回复
dickli1986 2010-04-08
你的Hibernate使用的延迟加载,当加载类的时候,session已经关闭了,所以无法加载那个类,在加载之前不要关闭session
回复
archko 2010-04-08
这一堆都没看出来有什么用,你不把相关的代码弄出来如何.
回复
相关推荐
发帖
Java EE
创建于2007-09-28

6.7w+

社区成员

J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
申请成为版主
帖子事件
创建了帖子
2010-04-08 06:08
社区公告
暂无公告