Java exception issue No Bean named ' sessionfactory ' is defined and initialize a collection of role

Source: Internet
Author: User

The Java "No bean named ' sessionfactory ' is defined" occurs frequently in the SHH framework or in the implementation of Hibernate SPING+JPA with annotation; The reason for the exception is the sessionfactory bean could not be found .

The reasons are as follows:

1, in Many-to-one or Many-to-many time we set the fetchtype= "lazy" delay loading. Solution to the problem in Web. XML, adding Opensessioninview filters, the main function of Opensessioninviewfilter is to bind a hibernate session to a line threads the complete request process. The goal is to implement the "Open Session in View" mode. For example: it allows

Delays loading the objects required to display after the transaction commits.
The Opensessioninviewfilter filter binds the Hibernate Session to the request thread, which is automatically detected by Spring's transaction manager. So the opensessioninviewfilter is suitable for Service layer

(One of the Filter-class note that I use hibernate so FILTER-CALSS:<filter-class> Org.springframework.orm.hibernate3.support.opensessioninviewfilter</filter-class>

If you are using JPA:<filter-class> Org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class > )

<!--to handle the  Opensessioninview filter, which must be configured before the Struts filter--><filter><filter-name> Opensessioninviewfilter</filter-name><filter-class> org.springframework.orm.hibernate3.support.opensessioninviewfilter</filter-class></filter>< filter-mapping><filter-name>opensessioninviewfilter</filter-name><url-pattern>/*</ Url-pattern></filter-mapping>


2, although the front has been added to the opensessioninviewfilter , but can also be reported sessionfactory not defited;

The reason is simple ———— name mismatch,opensessioninviewfilter mode uses bean.xml inside id= "sessionfactory" bean, when your sessionfactory bean Not called this name, it naturally reported that the bean could not find;

<bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >


Java exception issue No Bean named ' sessionfactory ' is defined and initialize a collection of role

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.