|
org.hibernate.LazyInitializationException问题的解决2008-07-26 11:18今日在struts2+spring+hibernate的环境中写个demo.在页面输出${parent.name}的时候报org.hibernate.LazyInitializationException: could not initialize proxy - no Session错误.
查了网上的解决方案,大都说需要在<many-to-one>里设置lazy="false".可是我设了以后问题还是没有解决.
web.xml中也加了spring的OpenSessionInViewFilter,配置如下:
<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>
最后发现是web.xml中配置的顺序问题造成的.这也可能是初学者常犯的一个错误.
在web.xml中我们应该将struts2的配置文件放在spring的配置后面.理解应该是让spring 在struts2的前面启动.才能管理到struts2.
我的web.xml详细配置如下:
<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>
<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>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> |
|