Java exception No bean named & #39; sessionFactory & #39; is defined and initialize a collection of role, sessionfactory

Source: Internet
Author: User

Java exception problem No bean named 'sessionfactory 'is defined and initialize a collection of role, sessionFactory

"No bean named 'sessionfactory 'is defined" in java frequently appears in the SHH framework or in the underlying layer of Sping + JPA using HIbernate annotation; the exception occurs because sessionFactory bean cannot be found.

The reasons are as follows:

1. We set fetchType = "lazy" for loading with delayed-to-one or delayed-to-minus. The solution is to add the openSessionInView filter to web. xml. The main function of OpenSessionInViewFilter is to bind a Hibernate Session to the thread corresponding to a complete request process. The purpose is to implement the "Open Session in View" mode. For example, it allows

After the transaction is committed, the system delays loading and displays the required objects.
The OpenSessionInViewFilter filters bind the Hibernate Session to the request thread, which is automatically detected by the Spring Transaction Manager. Therefore, OpenSessionInViewFilter applies to the Service layer

 

(For the filter-class, note that Hibernate is used for seat check, so filter-calss: <filter-class> org. springframework. orm. hibernate3.support. OpenSessionInViewFilter </filter-class>

If you use JPA: <filter-class> org. springframework. orm. jpa. support. OpenEntityManagerInViewFilter </filter-class>)

<! -- Used to handle delayed loading openSessionInView filter, this filter must be configured before the struts 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 OpenSessionInViewFilter has been added, sessionFactory not defited can also be reported;

The reason is very simple-the name does not match. In OpenSessionInViewFilter mode, the Bean id = "sessionFactory" in Bean. xml is used. If your sessionfactory Bean is not called this name, the Bean cannot be found;

<bean id="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">


 


No bean named 'sessionfactory 'is defined when the server is started

The bean initialization of DAO requires sessionFactory, and it cannot be found in all beans.
 
SSH Structure Problem: No bean named '/Index' is defined

<Bean name = "/index" class = "com. jqz. web. struts. action. IndexAction">
This class points to a wrong class. Hope to adopt it. Ask me again if you have any questions.

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.