开发者

When entityManagerFactory is not named "entityManagerFactory"

开发者 https://www.devze.com 2023-01-04 01:48 出处:网络
My webapp contains many entityManagerFactories (DBs). Each has its own unique name, such as entityManagerFactoryApp, entityManagerFactoryBusiness, entityManagerFactoryForum, etc.

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

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

W开发者_如何学Chen running Spring throws the following error:

NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined

But none of my entityManagerFactories is named entityManagerFactory.

I looked at the code of OpenEntityManagerInViewFilter and found setEntityManagerFactoryBeanName() method. How can I set it's value in web.xml?

Moreover, since I have multiple EMFs, should I define multiple OpenEntityManagerInViewFilters in web.xml?

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消