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

Basit

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

Volceri

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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

NoUniqueBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean

No qualifying bean of type 'javax.persistence.EntityManager' available: expected single matching bean but found 2

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

NoUniqueBeanDefinitionException: no qualifying bean of type ... i defined, expected single matching bean but found 2

No qualifying bean, expected single matching bean but found 2

Injection of persistence dependencies failed; No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined

No qualifying bean of type 'org.springframework.batch.core.Job' available: expected single matching bean but found 2:

No qualifying bean of type 'javax.persistence.EntityManagerFactory' available for OSGi Application

Spring JPA (Hibernate) No qualifying bean of type: javax.persistence.EntityManagerFactory

Spring Boot Embedded Tomcat - No qualifying bean of type 'javax.sql.DataSource' available: expected single matching bean but found 3

Spring Boot Error No qualifying bean of type 'javax.persistence.EntityManagerFactory' available

No qualifying bean of type [javax.persistence.EntityManager]

SpringFramework: expected single matching bean but found 2

No qualifying bean of type [] is defined

No qualifying bean of type [HrEmployeesReportOutput] found for dependency: expected at least 1 bean

Field dao in com.car.services.CarServices required a bean of type 'javax.persistence.EntityManagerFactory' that could not be found

No qualifying bean of type 'javax.persistence.EntityManager' when using Entity Manager with spring boot 2

Error creating bean entityManagerFactory, NoSuchMethodError: javax/persistence/Table.indexes

No qualifying bean of type [javax.sql.DataSource] is defined

Spring with MyBatis: expected single matching bean but found 2

Spring FactoryBean and autowiring not working : expected single matching bean but found 2

NoSuchBeanDefinitionException: No qualifying bean of type found

I am getting an error -- "No qualifying bean of type [hello.MessagePrinter] is defined"

No qualifying bean of type found for dependency in Spring Boot single table

No qualifying bean of type 'javax.sql.DataSource' available: more than one 'primary' bean found among candidates:

Spring @Autowire fails with No qualifying bean of type found for dependency error

Sprint Boot Data JPA: No qualifying bean of type 'java.util.Set<javax.persistence.EntityManager>' available

No qualifying bean of type UserRepository found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency

No qualifying bean of type [PATHTOCLASS] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency