Java unusual problem no Bean named & #39;sessionfactory& #39; is defined and initialize a collection of role

Source: Internet
Author: User

Left and right Java "No bean named ' sessionfactory ' is defined" now often go out shh or within its framework SPING+JPA use the bottom hibernate annotation implementation; The reason for the exception is The Sessionfactory bean could not be found .

The reasons are for example the following:

1, in Many-to-one or Many-to-many time we set the fetchtype= "lazy" delay loading.

The solution to this problem is to add the Opensessioninview filter in Web. XML, the main function of Opensessioninviewfilter is to bind a hibernate session and the corresponding line threads of a complete request process. The goal is to implement the "Open Session in View" mode.

For example: it agrees

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

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

Let's say you're using JPA:<filter-class> Org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class > )

<!--for handling delay load problems  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 Opensessioninviewfilter has been added earlier. But also can be reported sessionfactory not defited;

The reason is very easy ———— name mismatch,opensessioninviewfilter mode uses bean.xml inside the bean id= "sessionfactory". Assuming that your sessionfactory bean is not called this name, it naturally reports that the bean cannot be found;

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


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Java unusual problem no Bean named & #39;sessionfactory& #39; is defined and initialize a collection of role

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.