title: Spring jpa - can't use more than one EntityManagerFactory
body: <p>I'm working on a java spring mvc application that uses hibernate and jpa for persistence layer. I have 2 databases and I want map some entity tables in database1 and some in database2. So I have defined two configuration classes like this:</p>
  
  <p><strong>First:</strong></p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @EnableJpaRepositories(value = "com.mycompany.project.persistence.orm.module.common",entityManagerFactoryRef = "commonEntityManagerFactory",transactionManagerRef = "commonTransactionManager")
  public class PersistenceConfigCommon {
  
      public PersistenceConfigCommon() {
      }
  
  
      Properties additionalProperties() {
          return new Properties() {
              {  
                  setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
                  setProperty("hibernate.hbm2ddl.auto", "update");
                  setProperty("hibernate.show_sql", "true");
                  setProperty("hibernate.enable_lazy_load_no_trans", "true");
                  setProperty("hibernate.connection.CharSet", "utf8");
                  setProperty("hibernate.connection.characterEncoding", "utf8");
                  setProperty("hibernate.connection.useUnicode", "true");
              }
          };
      }
  
      @Primary
      @Bean(name = "commonEntityManagerFactory")
      public EntityManagerFactory commonEntityManagerFactory() {entityManagerFactoryBean");
          LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
          factory.setPersistenceUnitName("common");
          factory.setDataSource(commonDataSource());
          factory.setPackagesToScan("com.mycompany.project.persistence.orm.module");
  
          HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          vendorAdapter.setGenerateDdl(Boolean.TRUE);
          vendorAdapter.setShowSql(Boolean.TRUE);
          factory.setJpaVendorAdapter(vendorAdapter);
          factory.setJpaProperties(additionalProperties());
  
          factory.afterPropertiesSet();
          return factory.getObject();
      }
  
  
      @Primary
      @Bean(name = "commonDataSource")
      public DataSource commonDataSource() {
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
          dataSource.setDriverClassName("com.mysql.jdbc.Driver");
          dataSource.setUrl("jdbc:mysql://10.10.1.11:3306/database1?characterEncoding=UTF-8");
          dataSource.setUsername("fpuser");
          dataSource.setPassword("fpdb$123456");
  
          return dataSource;
      }
  
      @Primary
      @Bean(name = "commonTransactionManager")
      public PlatformTransactionManager commonTransactionManager() {
          JpaTransactionManager transactionManager = new JpaTransactionManager();
             transactionManager.setEntityManagerFactory(commonEntityManagerFactory());
          return transactionManager;
      }
  
      @Bean
      public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
          return new PersistenceExceptionTranslationPostProcessor();
      }
  
  }
  </code></pre>
  
  <p><strong>Second:</strong></p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @EnableJpaRepositories(value = "com.mycompany.project.persistence.orm.module.profile",entityManagerFactoryRef = "profileEntityManagerFactory",transactionManagerRef = "profileTransactionManager")
  public class PersistenceConfigProfile {
  
      public PersistenceConfigProfile() {
      }
  
  
      Properties additionalProperties() {
          return new Properties() {
              {  
                  setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
                  setProperty("hibernate.hbm2ddl.auto", "update");
                  setProperty("hibernate.show_sql", "true");
                  setProperty("hibernate.enable_lazy_load_no_trans", "true");
                  setProperty("hibernate.connection.CharSet", "utf8");
                  setProperty("hibernate.connection.characterEncoding", "utf8");
                  setProperty("hibernate.connection.useUnicode", "true");
              }
          };
       }
  
      @Primary
      @Bean(name = "profileEntityManagerFactory")
      public EntityManagerFactory profileEntityManagerFactory() {entityManagerFactoryBean");
          LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
          factory.setPersistenceUnitName("profile");
          factory.setDataSource(profileDataSource());
          factory.setPackagesToScan("com.mycompany.project.persistence.orm.module");
  
          HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          vendorAdapter.setGenerateDdl(Boolean.TRUE);
          vendorAdapter.setShowSql(Boolean.TRUE);
          factory.setJpaVendorAdapter(vendorAdapter);
          factory.setJpaProperties(additionalProperties());
  
          factory.afterPropertiesSet();
          return factory.getObject();
      }
  
  
      @Primary
      @Bean(name = "profileDataSource")
      public DataSource profileDataSource() {
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
          dataSource.setDriverClassName("com.mysql.jdbc.Driver");
          dataSource.setUrl("jdbc:mysql://10.10.1.63:3306/database2?characterEncoding=UTF-8");
          dataSource.setUsername("fpuser");
          dataSource.setPassword("fpdb$123456");
  
          return dataSource;
      }
  
      @Primary
      @Bean(name = "profileTransactionManager")
      public PlatformTransactionManager commonTransactionManager() {
          JpaTransactionManager transactionManager = new JpaTransactionManager();
             transactionManager.setEntityManagerFactory(commonEntityManagerFactory());
          return transactionManager;
      }
  
      @Bean
      public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
          return new PersistenceExceptionTranslationPostProcessor();
      }
  
  }
  </code></pre>
  
  <p>Note everything is similar in these two files, just <code>common</code> has been converted to <code>profile</code> in the second file and database name and ip has been changed. Also in the DAOs I have put this:</p>
  
  <pre><code>@PersistenceContext(unitName = "common") //or "profile"
  protected EntityManager entityManager;
  
  @Override
  public EntityManager getEntityManager() {
      return entityManager;
  }
  </code></pre>
  
  <p>Now, when I run the project, I get the following exception:</p>
  
  <pre><code> org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2: commonEntityManagerFactory,profileEntityManagerFactory
  </code></pre>
  
  <p>I have bee stuck in this exception for 2 days. Could anyone help me to solve this problem. Thanks for your time.</p>