Spring / Hibernate NoUniqueBeanDefinitionException

时间:2014-10-31 16:56:27

标签: java spring hibernate

我在Hibernate中使用Spring持久性。我收到以下错误:

org.springframework.beans.factory.NoUniqueBeanDefinitionException: 
  No qualifying bean of type [javax.persistence.EntityManagerFactory] is 
  defined: 
  expected single matching bean but found 2: 
  entityManagerFactory,previewEntityManagerFactory

我的.spring.xml文件包含

<!-- JPA Entity Manager Factory -->
<bean id="entityManagerFactory" lazy-init="true" depends-on="liquibase"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
      p:dataSource-ref="dataSource">
    <property name="persistenceUnitName" value="examStorePersistenceUnit" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="${database.generateDdl}"/>
            <property name="showSql" value="${database.showSql}"/>
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
        </props>
    </property>
</bean>

<!-- JPA Entity Manager Factory -->
<bean id="previewEntityManagerFactory" lazy-init="true" depends-on="liquibase"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        p:dataSource-ref="dataSourcePreview">
    <property name="persistenceUnitName" value="previewPersistenceUnit" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="${database.generateDdl}"/>
            <property name="showSql" value="${database.showSql}"/>
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
        </props>
    </property>
</bean>
.
.
.
<bean id="dataSource"
      class="org.springframework.jndi.JndiObjectFactoryBean"
      lazy-init="true">
    <property name="jndiName" value="java:comp/env/jdbc/minerva"/>
</bean>
<!--jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/minerva" default-ref="examDataSource" /-->
<bean id="dataSourcePreview"
        class="org.springframework.jndi.JndiObjectFactoryBean"
        lazy-init="true">
    <property name="jndiName" value="java:comp/env/jdbc/minerva"/>
</bean>
.
.
.
<!-- Transaction Config -->
<bean id="transactionManager" lazy-init="true"
      class="org.springframework.orm.jpa.JpaTransactionManager"
      p:entityManagerFactory-ref="entityManagerFactory"/>
<bean id="prevueTransactionManager" lazy-init="true"
      class="org.springframework.orm.jpa.JpaTransactionManager"
      p:entityManagerFactory-ref="previewEntityManagerFactory"/>

我的Dao包含

public class BaseDao<T extends Persistable> implements Dao<T> {

    @SuppressWarnings("ProtectedField")
    @PersistenceContext( unitName="examStorePersistenceUnit")
    @Qualifier("entityManagerFactory")
    protected EntityManager entityManager;

    @SuppressWarnings("ProtectedField")
    @PersistenceContext( unitName="previewPersistenceUnit")
    @Qualifier("previewEntityManagerFactory")
    protected EntityManager previewEntityManager;

请注意,我们未使用OpenEntityMangerInViewFilter

导致NoUniqueBeanDefinitionException的原因是什么?

请注意Dao bean发生错误:

Caused by: org.springframework.beans.factory.BeanCreationException: 
    Error creating bean with name 'itemReportingGroupDaoImpl': 
    Injection of persistence dependencies failed; 
    nested exception is 
        org.springframework.beans.factory.NoUniqueBeanDefinitionException: 
    No qualifying bean of type [javax.persistence.EntityManagerFactory] is 
        defined: 
    expected single matching bean but found 2: 
        entityManagerFactory,previewEntityManagerFactory

下面是更多的堆栈跟踪。

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'itemReportingGroupDaoImpl': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2: entityManagerFactory,previewEntityManagerFactory
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:356)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1021)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:964)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:862)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:481)
    ... 101 more
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2: entityManagerFactory,previewEntityManagerFactory
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findDefaultEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:556)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:515)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityManager(PersistenceAnnotationBeanPostProcessor.java:682)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:655)
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:164)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:353)
    ... 112 more

0 个答案:

没有答案