2014-03-28 58 views
5

好之後確定,因爲LazyInitializationException中,我需要添加「OpenSessionViewInFilter」在我的web.xml中,用下面的代碼:名爲「SessionFactory的」無豆是的OpenSessionInViewFilter

<!-- openSessionInViewfilter --> 
<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> 

問題是我加入此之後代碼中,我得到了以下錯誤,當我嘗試啓動Tomcat 7:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1094) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:276) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196) 
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1079) 
    at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:242) 
    at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:227) 
    at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:171) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:724) 

我不知道,知道爲什麼這個錯誤occurrs因爲我使用的EntityManager(JPA),而不是SessionFactory的,看到我的applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 

    <!-- Seta anotaçoes para serem usadas pelo Spring --> 
    <context:annotation-config /> 

    <!-- Define o pacote onde o Spring vai procurar por beans anotados --> 
    <context:component-scan 
     base-package="br.com.sender" /> 

    <!-- define que as transaçoes irao ser anotadas --> 
    <tx:annotation-driven proxy-target-class="true" /> 

    <!-- Configuracao do Banco de Dados --> 
    <bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="org.postgresql.Driver" /> 
     <property name="url" value="jdbc:postgresql://localhost/sender" /> 
     <property name="username" value="postgres" /> 
     <property name="password" value="pgadmin" /> 
    </bean> 

    <!-- Configuracao do Hibernate --> 
    <bean id="entityManagerFactory" 
     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="persistenceUnitName" value="senderPU" /> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
       <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" /> 
       <property name="showSql" value="true" /> 
      </bean> 
     </property> 
    </bean> 

    <!-- Configuracao do gerente de transacoes do Spring --> 
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    </bean> 

</beans> 

回答

8

你需要下面

<filter> 
    <filter-name>OpenEntityManagerInViewFilter</filter-name> 
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class> 
</filter> 

<filter-mapping> 
    <filter-name>OpenEntityManagerInViewFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

添加這兩個片段然後,你需要彈簧orm- {我們在這裏的版本}的.jar添加到類路徑。像這樣的,如果你使用maven

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-orm</artifactId> 
    <version>${spring.ver}</version> 
</dependency> 

- 編輯 -

對不起,我使用JPA,您的代碼建議你使用Hibernate3的讓你的過濾器類應該是org.springframework .orm.hibernate3.support.OpenSessionInViewFilter

+0

謝謝你的工作。這個OpenEntityManagerInViewFilter特定於JPA,另一個是HIBERNATE? –

+0

@ Mr.Lanhellas請檢查我上面的編輯。 :) – Desorder

+0

我也使用JPA,與休眠,但使用EntityManager。 –

相關問題