OpenSessionInViewFilter在SSH框架中的应用

来源:互联网 发布:java unicode编码 编辑:程序博客网 时间:2024/05/23 12:16

欢迎访问北大青鸟大连鑫鼎中心 www.0411bdqn.com

延迟初始化错误是运用Hibernate开发项目时最常见的错误。如果对一个类或者集合配置了延迟检索策略,那么必须当代理类实例或代理集合处于持久化状态(即处于Session范围内)时,才能初始化它。如果在游离状态时才初始化它,就会产生延迟初始化错误

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
  
   <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
  
<filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

注意:struts插件不能留在Struts配置文件中、否则OpenSessionInViewFilter失效

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
   <set-property
    property="contextConfigLocation"
    value="classpath:applicationContext.xml" />
</plug-in>

通过以上配置即可解决Hibernate延迟加载问题(页面异常:could not initialize proxy - the owning Session was closed)

原创粉丝点击