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

后端 未结 3 1467
青春惊慌失措
青春惊慌失措 2020-12-06 00:48

I am spring spring 3.2. Here is my config file

 

        
相关标签:
3条回答
  • 2020-12-06 01:19

    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.

    0 讨论(0)
  • 2020-12-06 01:27

    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>
    
    0 讨论(0)
  • 2020-12-06 01:33

    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".

    0 讨论(0)
提交回复
热议问题