Java 没有名为'的bean;sessionFactory';在OpenSessionInViewFilter之后定义
因为LazyInitializationException,我需要在我的web.xml中添加“OpenSessionViewInfiller”,代码如下:Java 没有名为'的bean;sessionFactory';在OpenSessionInViewFilter之后定义,java,spring,hibernate,jpa,jsf-2,Java,Spring,Hibernate,Jpa,Jsf 2,因为LazyInitializationException,我需要在我的web.xml中添加“OpenSessionViewInfiller”,代码如下: <!-- openSessionInViewfilter --> <filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class>org.springfra
<!-- 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>
我不知道为什么会发生此错误,因为我使用的是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>
您需要在下面添加两个代码段
<filter>
<filter-name>OpenEntityManagerInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
OpenEntityManager视图过滤器
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
及
OpenEntityManager视图过滤器
/*
然后,您需要将spring orm-{our version here}.jar添加到您的类路径中。如果你用的是maven
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.ver}</version>
</dependency>
org.springframework
春季甲虫
${spring.ver}
--编辑--
抱歉,我使用的是JPA,您的代码表明您使用的是hibernate3,因此您的筛选器类应该是org.springframework.orm.hibernate3.support.OpenSessionInViewFilter,谢谢您的帮助。此OpenEntityManagerViewFilter特定于JPA,另一个特定于HIBERNATE?@Mr.Lanhellas请检查我上面的编辑:)我在hibernate中也使用JPA,但在EntityManager中使用。@Mr.Lanhellas我在hibernate实现中使用JPA,我的第一个代码片段应该适合您。:)我尝试过,但我有另一个问题:在登录应用程序后,当我尝试调用lazy属性时,我遇到了LazyInitializationException(会话已关闭)。
<filter-mapping>
<filter-name>OpenEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.ver}</version>
</dependency>