【问题标题】:Use OpenEntityManagerInViewFilter in my project?在我的项目中使用 OpenEntityManagerInViewFilter?
【发布时间】:2012-02-15 22:25:26
【问题描述】:

由于休眠的延迟加载问题,我尝试设置弹簧 OpenEntityManagerInViewFilter。

但我无法让它与我已经工作的应用程序一起工作。 为了使用 Open EM,除了向 web.xml 添加内容和创建 applicationContext.xml 之外,我还需要做什么?

谢谢

我已经添加到 web.xml:

    <filter>
        <filter-name>
            OpenEntityManagerInViewFilter
        </filter-name>
            <filter-class>
                org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
            </filter-class>
            <init-param>
                <param-name>singleSession</param-name>
                <param-value>true</param-value>
            </init-param>
            <init-param>
                <param-name>flushMode</param-name>
                <param-value>AUTO</param-value>
            </init-param>
    </filter>
    <!-- Include this if you are using Hibernate -->
    <filter-mapping>
        <filter-name>OpenEntityManagerInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Spring config -->
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

    <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>

还有一个 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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

</beans>

我已经可以部署我的应用了,但是当我尝试启动它时会抛出 ex:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined

【问题讨论】:

    标签: spring jsf jakarta-ee entitymanager


    【解决方案1】:

    像这样在 applicationContext.xml 中创建一个 entityManagerFactory:

    <!-- Add JPA support -->
     <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="loadTimeWeaver">
            <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
        </property>
     </bean>
    
     <!-- Add Transaction support -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    

    【讨论】:

      【解决方案2】:

      正如错误所示,您的 spring 配置文件中没有定义 entityManagerFactory。或者它是用其他名称定义的。尝试将此初始化参数添加到 web.xml 中的配置中

      <init-param>
          <param-name>entityManagerFactoryBeanName</param-name>
          <param-value>entityManagerFactory</param-value>
      </init-param>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-21
        • 2016-03-30
        • 2018-03-26
        • 1970-01-01
        • 1970-01-01
        • 2011-01-26
        • 2013-08-07
        相关资源
        最近更新 更多