views:

43

answers:

1

My webapp contains many entityManagerFactories (DBs) , each has its unique name , such as entityManagerFactoryApp , entityManagerFactoryBusiness , entityManagerFactoryForum ...etc.

In a webapp , I have to add OpenEntityManagerInViewFilter to achieve "Open Session in View" pattern.

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

But when running , Spring complains NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined

But my EMFs are not named "entityManagerFactory" , I lookup OpenEntityManagerInViewFilter's code and find setEntityManagerFactoryBeanName() method , but how should I set its value in web.xml ? Moreover , I have multiple EMFs , shoild I define multiple OpenEntityManagerInViewFilters in web.xml ?

Thanks in advanced.

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

+2  A: 

OpenEntityManagerInViewFilter is a subclass of GenericFilterBean, which automatically translates filter init parameters into bean properties. So to set the entityManagerFactoryBeanName property on the filter, just use an 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>

You'll need one of these per EntitymanagerFactory, with the entityManagerFactoryBeanName set accordingly.

skaffman
Thank you , it works!
smallufo