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

青春壹個敷衍的年華 提交于 2021-01-27 04:49:38

问题


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

回答1:


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)




回答2:


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




回答3:


Somewhere you have a bean of type org.springframework.flex.core.io.JpaHibernateConfigProcessor, and it looks like it has an init method that gets autowired with a javax.persistence.EntityManagerFactory. Since there are two of them, it fails. It probably is explicitly declared in XML somewhere like:

<bean class="org.springframework.flex.core.io.JpaHibernateConfigProcessor"/>

Note there is no id attribute, hence the long bean name ending in "#0".

Edit: On second thought, the name of that class looks like some kind of bean processor that runs on ApplicationContext startup, so perhaps it's created by a custom spring namespace element or some other mechanism.




回答4:


You must do all stuff first like defining dataSources eventManagerFactory transactionManagers persistenceUnits(must be in separate persistence.xml file) etc and then compile it will work fine also dont forget to add annotations in your baseDao class as @applicationContext(unitName="example")



来源:https://stackoverflow.com/questions/6695914/no-unique-bean-of-type-javax-persistence-entitymanagerfactory-is-defined-expe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!