getting error No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2
Asked Answered
A

3

17

I am spring spring 3.2. Here is my config file

 <bean id="legacyDataSource" name="legacydb" class="org.springframework.jdbc.datasource.DriverManagerDataSource" lazy-init="true">
    <property name="driverClassName" value="${jdbc.legacy.driverClassName}" />
    <property name="url" value="${jdbc.legacy.url}" />  
    <property name="username" value="${jdbc.legacy.username}" />
    <property name="password" value="${jdbc.legacy.password}" />
</bean>

 <bean id="ls360DataSource" name="Ls360db" class="org.springframework.jdbc.datasource.DriverManagerDataSource" lazy-init="true" >
    <property name="driverClassName" value="${jdbc.ls360.driverClassName}" />
    <property name="url" value="${jdbc.ls360.url}" />  
    <property name="username" value="${jdbc.ls360.username}" />
    <property name="password" value="${jdbc.ls360.password}" />
</bean>

<bean id="legacyTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="legacyEmf"/>
</bean>

<bean id="ls360TransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="ls360Emf"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="legacyEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
    <property name="dataSource" ref="legacyDataSource" />
    <property name="jpaVendorAdapter" ref="vendorAdaptor" />         
    <property name="packagesToScan" value="com.softech.ls360.integration.regulators.plcb.domain"/>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
            <prop key="hibernate.max_fetch_depth">3</prop>
            <prop key="hibernate.jdbc.fetch_size">50</prop>
            <prop key="hibernate.jdbc.batch_size">10</prop>
            <prop key="hibernate.show_sql">true</prop>              
        </props>        
    </property>
</bean>   

<bean id="ls360Emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
    <property name="dataSource" ref="ls360DataSource" />
    <property name="jpaVendorAdapter" ref="vendorAdaptor" />         
    <property name="packagesToScan" value="com.softech.ls360.integration.regulators.plcb.domain"/>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
            <prop key="hibernate.max_fetch_depth">3</prop>
            <prop key="hibernate.jdbc.fetch_size">50</prop>
            <prop key="hibernate.jdbc.batch_size">10</prop>
            <prop key="hibernate.show_sql">true</prop>              
        </props>        
    </property>
</bean>
<context:component-scan base-package="....db" />

Here is my class

@Service("dbManager") 
@Repository
@Transactional
public class DatabaseManager {

    @PersistenceContext
    @Qualifier("legacyEmf")
    private EntityManager legacyEm;

    @PersistenceContext
    @Qualifier("ls360Emf")
    private EntityManager ls360Em;

    @SuppressWarnings("unchecked")
    @Transactional(readOnly=true)
    public List<Object> getResultList(String query, Class mappingClass) throws Exception {

        //Query emQuery = legacyEm.createNativeQuery(query, mappingClass);

        //return  emQuery.getResultList();
        return null;

    } //end of findTraineeFromLegacy()
}

Now when i rum the code i get the following error

Error creating bean with name 'dbManager': 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: legacyEmf,ls360Emf

Why i am getting this error. How can i solve it?

Thanks

Angelenaangeleno answered 24/7, 2013 at 11:48 Comment(1)
same issue here, but the proposed solution doesn't work for unit-testing. Did you try spring junit test ?Siward
R
23

I had the same issue today. Solved it doing the following:

First I've added the parameter unitName to @PersistenceContext to both entity manager properties:

@PersistenceContext(unitName="appPU")
@Qualifier(value = "appEntityManagerFactory")
private EntityManager appEntityManager;

@PersistenceContext(unitName="managerPU")
@Qualifier(value = "managerEntityManagerFactory")
private EntityManager managerEntityManager;

And in my configuration file I've added a property persistenceUnitName to my bean definitions:

<bean id="appEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource1" />
    <property name="persistenceUnitName" value="appPU" />
    <property name="packagesToScan" value="br.com.app.domain" />
    ...
</bean>

<bean id="managerEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource2" />
    <property name="persistenceUnitName" value="managerPU" />
    <property name="packagesToScan" value="br.com.app.domain" />
    ...
</bean>
Repartee answered 6/8, 2013 at 7:1 Comment(3)
This is weird because I'm having the exactly same problem today but the @Qualifier does not work =(. I'm pretty sure all my configurations are all right, could you please post your entire applicationContext? Thank you.Mallet
The solution does not work for junit test (AbstractTransactionalJUnit4SpringContextTests), any idea why ?Siward
Thanks a lot! It worked for me. Using @PersistenceContex with unitName is enough to solve the injection. It's not necessary to use the @Qualifier because you are injecting the EntityManager instead the EntityManagerFactoryUri
A
4

Also I'd like to add once more useful comment: you need to extend the section in the 'web.xml' file of your web-app. Since now you have 2 Entity Managers, you need 2 OpenEntityManagerInViewFilters. Look the example:

<filter>
  <filter-name>OpenEntityManagerInViewFilter1</filter-name>
  <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
     <init-param>
         <param-name>entityManagerFactoryBeanName</param-name>
         <param-value>appEntityManagerFactory</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>OpenEntityManagerInViewFilter1</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>


<filter>
  <filter-name>OpenEntityManagerInViewFilter2</filter-name>
  <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
    <init-param>
        <param-name>entityManagerFactoryBeanName</param-name>
        <param-value>managerEntityManagerFactory</param-value>
    </init-param>
  </filter>

<filter-mapping>

<filter-name>OpenEntityManagerInViewFilter2</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>

Pay attention on fact the name 'appEntityManagerFactory' in < param-value >appEntityManagerFactory< / param-value > = 'appEntityManagerFactory' in < bean id="appEntityManagerFactory".

Aweigh answered 12/9, 2013 at 10:39 Comment(0)
B
2

I also faced such problems and solved it. Please do the following to solve this error:

Add the following line to all your entity classes of both schema.

@PersistenceContext(unitName="<persistenceUnit>")
transient EntityManager entityManager;

<persistenceUnit> is the name of the persistence unit you defined in the persistence.xml file.

Boudicca answered 25/2, 2014 at 6:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.