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