当entityManagerFactory未命名为“entityManagerFactory”时

时间:2010-06-21 21:51:38

标签: java spring servlets jpa servlet-filters

我的webapp包含许多entityManagerFactories(DB),每个都有其唯一的名称,例如entityManagerFactoryApp,entityManagerFactoryBusiness,entityManagerFactoryForum ...等。

在webapp中,我必须添加OpenEntityManagerInViewFilter来实现“在视图中打开会话”模式。

<filter>
    <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

但是在跑步时,Spring抱怨NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined

但是我的EMF没有命名为“entityManagerFactory”,我查找OpenEntityManagerInViewFilter的代码并查找setEntityManagerFactoryBeanName()方法,但是我应该如何在web.xml中设置它的值? 此外,我有多个EMF,我在web.xml中定义了多个OpenEntityManagerInViewFilter

先谢谢。

环境:Spring 3.0.2,Hibernate 3.5.1.Final,Resin 4.0.7,JPA2(hibernate-jpa-2.0-api)

1 个答案:

答案 0 :(得分:5)

OpenEntityManagerInViewFilterGenericFilterBean的子类,它自动将过滤器init参数转换为bean属性。因此,要在过滤器上设置entityManagerFactoryBeanName属性,只需使用init-param:

<filter>
   <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
   <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
   <init-param>
      <param-name>entityManagerFactoryBeanName</param-name>      
      <param-value>entityManagerFactoryApp</param-value>       
   </init-param>
</filter>

根据EntitymanagerFactory,您需要其中一个,并相应地设置entityManagerFactoryBeanName