Configuration and function of opensessioninviewfilter

Source: Internet
Author: User

Spring solves the issue of closing and enabling hibernate sessions for us.
Hibernate allows delayed loading of associated objects and attributes, but must ensure that the delayed loading operation is limited to the same hibernate session. If the service layer returns a domain object with the delayed Loading Function enabled to the web layer, when the web layer accesses the data that requires delayed loading, the hibernate session for the domain object loading has been disabled, these causes access exceptions for delayed data loading.

(Eg: org. hibernate. lazyinitializationexception :( lazyinitializationexception. Java: 42)
-Failed to lazily initialize a collection of Role: CN. easyjava. Bean. Product. producttype. childtypes, no session or session was closed
Org. hibernate. lazyinitializationexception: failed to lazily initialize a collection of Role: CN. easyjava. Bean. Product. producttype. childtypes, no session or session was closed ).

It is used to bind a hibernate session to the thread corresponding to a complete request process. The purpose is to implement the "open session in view" mode. For example, it allows delayed loading of the desired objects after the transaction is committed.


The opensessioninviewfilter provided by spring solves this problem well. The main function of opensessioninviewfilter is to bind a hibernate session to the thread corresponding to a complete request process. The purpose is to implement the "open session in view" mode. For example, it allows delayed loading of the desired objects after the transaction is committed.
The opensessioninviewfilter filters bind the hibernate session to the request thread, which is automatically detected by the spring Transaction Manager. Therefore, opensessioninviewfilter is applicable to the service layer's transaction management environment using hibernatetransactionmanager or jtatransactionmanager. It can also be used in non-transaction read-only data operations.

<Filter>
<Filter-Name> spring opensessioninviewfilter </filter-Name>
<Filter-class> org. springframework. Orm. hibernate3.support. opensessioninviewfilter </filter-class>
<Init-param>

<! --
Specify the name of org. springframework. Orm. hibernate3.localsessionfactorybean in the spring configuration file. The default value is sessionfactory.
If the name of localsessionfactorybean in spring is not sessionfactory, you must specify this parameter. Otherwise, the exception of sessionfactory cannot be found.
-->
<Param-Name> sessionfactorybean </param-Name>
<Param-value> sessionfactory </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-Name> spring opensessioninviewfilter </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>

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.