Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2

I am defining two EntityManager beans for two different databases. Each EntityManager bean refers to a unique, respective <persistence-unit/> defined in persistence.xml.

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:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2

Has anybody faced this issue? Am I missing something while upgrading to Spring 3? I'd really appreciate any replies.

I'm using Spring 3 with Hibernate and JPA.

The problem isn't ambiguity; I'm declaring two EntityManagerFactory beans and injecting them as follows:

  <bean id="oracleJpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
   <property name="entityManagerFactory" ref="entityManagerFactory"></property>
  </bean>

  <bean id="sqlJpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
   <property name="entityManagerFactory" ref="sqlEntityManagerFactory"></property>
  </bean>

This is the full stack trace:

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
like image 494
Leela Addagulla Avatar asked Jul 14 '11 15:07

Leela Addagulla


2 Answers

Inject your EntityManager like this:

@PersistenceContext(unitName = "unit1")
private EntityManager entityManager;

or your EntityManagerFactory like this:

@PersistenceUnit(unitName = "unit1")
private EntityManagerFactory entityManagerFactory;

(You will probably need <context:annotation-config/> in your context for this to work)

like image 73
Sean Patrick Floyd Avatar answered Sep 19 '22 02:09

Sean Patrick Floyd


My Problem is solved. spring-flex-core library was the culprit. The version 1.5.0.M1 that I am using does not allow Multiple EntityManagerFactories. When I used 1.5.0.RELEASE, the error disappeared.The following article was helpful http://forum.springsource.org/showthread.php?100273-JpaHibernateConfigProcessor-complains-when-multiple-EntityManagers-present

like image 31
Leela Addagulla Avatar answered Sep 22 '22 02:09

Leela Addagulla