Spring consolidation hibernate using lazy load error could not initialize proxy-no session_ consolidation-spring

Source: Internet
Author: User

Today, when I was learning Shiro, I built a springmvc+spring+hibernate integration framework, and then I made an error when I ran the one-many,many-one mapping relationship with lazy loading.

Could not initialize proxy-no the session and then went on the internet to find data, which means that since Hibernate and spring are integrated, the hibernate session is given to spring management and the request comes in to open SE Ssion, the session is closed when the request completes. When we want to use lazy load to get data, the original session is closed and no more data can be obtained. Thus, spring specifically makes a filter org.springframework.orm.hibernate4.support.OpenSessionInViewFilter for this situation. It can maintain the declaration cycle of the hibernate session between the opening and closing of the view. In this way, as long as our view is not closed, we can use Ajax to get the data through lazy loading.

The specific configuration is as follows:

<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

This is the HIBERNATE4 configuration, to be configured in Web.xml and because XML loading is in a certain order, so try to write it up.

After the configuration is complete, you can use it happily.

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.