没有定义[javax.persistence.EntityManagerFactory]类型的唯一bean:期望单个bean,但找到2 [英] No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2

查看:493
本文介绍了没有定义[javax.persistence.EntityManagerFactory]类型的唯一bean:期望单个bean,但找到2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为两个不同的数据库定义两个EntityManager bean.每个EntityManager bean均引用在persistence.xml中定义的唯一的各个<persistence-unit/>.

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

相同的代码和配置在Spring 2.x中运行良好.当我升级到Spring 3时,在服务器中部署应用程序时看到以下异常:

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

有人遇到这个问题吗?升级到Spring 3时我会缺少什么吗? 非常感谢您的答复.

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

我正在将Spring 3与Hibernate和JPA结合使用.

I'm using Spring 3 with Hibernate and JPA.

问题不是模棱两可;我声明了两个EntityManagerFactory bean并按如下方式注入它们:

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>

这是完整的堆栈跟踪:

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

推荐答案

我的问题已解决.罪魁祸首是spring-flex-core库.我正在使用的版本1.5.0.M1不允许使用多个EntityManagerFactories.当我使用1.5.0.RELEASE时,错误消失了. http://forum.springsource.org/showthread .php?100273-JpaHibernateConfigProcessor-complains-when-multiple-EntityManagers-present

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

这篇关于没有定义[javax.persistence.EntityManagerFactory]类型的唯一bean:期望单个bean,但找到2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆