If the open session in view provided by spring is not used, the session must be closed at the service (or Dao) layer, So If lazy loading is set to true, link set should be initialized at the application layer, such as company. getemployees (); otherwise, Hibernate will throw session already closed exception;
Open session in view provides a simple method to better solve the lazy loading problem.
It has two Configuration Methods: opensessioninviewinterceptor and opensessioninviewfilter (see springside for details). The functions are the same, but one is configured in Web. XML and the other is configured in application. xml.
Open session in view keeps the hibernate session open during the request binding session to the current thread, so that the session can be used throughout the request, for example, in the view layer, PO can also lazy loading data, for example, $ {company. employees }. When the view layer logic is complete, the session will be automatically closed through the dofilter method of filter or the posthandle method of interceptor.
Opensessioninviewinterceptor Configuration:
<Beans>
<Bean name = "opensessioninviewinterceptor" class = "org. springframework. Orm. hibernate3.support. opensessioninviewinterceptor">
<Property name = "sessionfactory">
<Ref bean = "sessionfactory"/>
</Property>
</Bean>
<Bean id = "urlmapping" class = "org. springframework. Web. servlet. handler. simpleurlhandlermapping">
<Property name = "interceptors">
<List>
<Ref bean = "opensessioninviewinterceptor"/>
</List>
</Property>
<Property name = "mappings">
....
</Property>
</Bean>...
</Beans>
Opensessioninviewfilter Configuration:
<Web-app>
<Filter>
<Filter-Name> hibernatefilter </filter-Name>
<Filter-class> org. springframework. Orm. hibernate3.support. opensessioninviewfilter </filter-class>
<! -- The default value of singlesession is true. If it is set to false, opensessioninview is useless. -->
<Init-param>
<Param-Name> singlesession </param-Name>
<Param-value> true </param-value>
</Init-param>
</Filter>...
<Filter-mapping>
<Filter-Name> hibernatefilter </filter-Name>
<URL-pattern> *. DO </url-pattern>
</Filter-mapping>
...
</Web-app>