Delayed loading solution: opensessioninviewinterceptor and opensessioninviewfilter configurations

Source: Internet
Author: User

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>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.