title: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2
body: <p>I am defining two <code>EntityManager</code> beans for two different databases. Each <code>EntityManager</code> bean refers to a unique, respective <code>&lt;persistence-unit/&gt;</code> defined in <code>persistence.xml</code>.</p>
  
  <p>The same code and config worked fine with Spring 2.x.  When I upgrade to Spring 3, I see the following exception while deploying the app in the server:</p>
  
  <p><code>Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2</code></p>
  
  <p>Has anybody faced this issue? Am I missing something while upgrading to Spring 3?
  I'd really appreciate any replies.</p>
  
  <p>I'm using Spring 3 with Hibernate and JPA.</p>
  
  <p>The problem isn't ambiguity; I'm declaring two <code>EntityManagerFactory</code> beans and injecting them as follows: </p>
  
  <pre><code>  &lt;bean id="oracleJpaTemplate" class="org.springframework.orm.jpa.JpaTemplate"&gt;
     &lt;property name="entityManagerFactory" ref="entityManagerFactory"&gt;&lt;/property&gt;
    &lt;/bean&gt;
  
    &lt;bean id="sqlJpaTemplate" class="org.springframework.orm.jpa.JpaTemplate"&gt;
     &lt;property name="entityManagerFactory" ref="sqlEntityManagerFactory"&gt;&lt;/property&gt;
    &lt;/bean&gt;
  </code></pre>
  
  <p>This is the full stack trace:</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.flex.core.io.JpaHibernateConfigProcessor#0': Invocation of init method failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1412)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
      ... 59 more
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2
      at org.springframework.beans.factory.BeanFactoryUtils.beanOfTypeIncludingAncestors(BeanFactoryUtils.java:309)
      at org.springframework.flex.core.io.JpaHibernateConfigProcessor.afterPropertiesSet(JpaHibernateConfigProcessor.java:21)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409)
      ... 66 more
  </code></pre>