gpt4 book ai didi

hibernate - 未定义 [javax.persistence.EntityManagerFactory] ​​类型的唯一 bean : expected single bean but found 2

转载 作者:行者123 更新时间:2023-12-02 23:41:21 27 4
gpt4 key购买 nike

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

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

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 时我是否遗漏了某些内容?我真的很感激任何回复。

我将 Spring 3 与 Hibernate 和 JPA 结合使用。

问题不在于含糊不清;而是在于。我声明两个 EntityManagerFactory bean 并注入(inject)它们,如下所示:

  <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

最佳答案

像这样注入(inject)你的EntityManager:

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

或者你的EntityManagerFactory像这样:

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

(您可能需要 <context:annotation-config/> 在您的上下文中才能正常工作)

关于hibernate - 未定义 [javax.persistence.EntityManagerFactory] ​​类型的唯一 bean : expected single bean but found 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6695914/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com