In Spring and Hibernate integration, sessionFactory is not injected after OpenSessionInViewFilter is used.

Source: Internet
Author: User

Recently, I have seen a sentence in zhihu. One of the ways to keep learning is that you have seen more cool people who are unwilling to do so.


When Spring and hibernate are integrated, jsp pages are displayed and the display attributes appear:


Org. apache. jasper. JasperException: cocould not initialize proxy-no Session-Class: org. hibernate. proxy. AbstractLazyInitializer
File: AbstractLazyInitializer. java

No session, lazy loading, adding an attribute such as name displayed on the jsp page is actually obtained using the getName method, but the session has been closed.


The solution is to write one more filter and the name is intuitive.

      
  
   openSessionInview 
       
  
   org.springframework.orm.hibernate3.support.OpenSessionInViewFilter 
     
 
           
  
    openSessionInview
            
  
    /*
   
  
 



When the session is opened at the presentation layer, it must be written before struts filtering. Because of the existence of the responsibility chain, it is first read but then implemented.


When I felt that there was no problem after the configuration, I found that sessionFactory was not injected, because the id of my sessionFactory was abbreviated to sf for me, and then I guess, it should be the OpenSessionInViewFilter class which needs to be injected with sessionFactory. This class also has the get method, but the name does not match, so the injection fails.


Try later

           
  
 

Of course not. I checked it later. The correct configuration is initialized in web. xml.

      
  
   openSessionInview 
       
  
   org.springframework.orm.hibernate3.support.OpenSessionInViewFilter 
       
             
   
     sessionFactoryBeanName
             
   
     sf
        
  
 
           
  
    openSessionInview
            
  
    /*
   
  
 

Finally, the name attribute is successfully displayed on the jsp page.



Related Article

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.