在用到【fetch=FetchType.LAZY】的时候,会遇到一些问题。
比方说SpringMVC,在后台(M层)加载完数据以后,session就会随之关闭。这个时候由于懒加载(LAZY)的数据在后台没有被用到,所以没有获取。
如果在前台JSP(V层)想使用懒加载数据的时候,由于后台没有获取到想要加载的数据,系统就会报错。
为了在V层使用懒加载的数据,在前台读取数据的时候,session就必须是打开的。
这就是必须要用到 OpenSessionInViewFilter。
web.xml:
比方说SpringMVC,在后台(M层)加载完数据以后,session就会随之关闭。这个时候由于懒加载(LAZY)的数据在后台没有被用到,所以没有获取。
如果在前台JSP(V层)想使用懒加载数据的时候,由于后台没有获取到想要加载的数据,系统就会报错。
为了在V层使用懒加载的数据,在前台读取数据的时候,session就必须是打开的。
这就是必须要用到 OpenSessionInViewFilter。
web.xml:
<pre name="code" class="html"> <!--Hibernate OpenSessionInView配置-->
<!--OpenSessionInView会有性能上的损失 -->
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--Hibernate OpenSessionInView配置结束-->