title: Spring JPA multiple EntityManagerFactory defined but still repositories accessing only one database
body: <p>Everyone says its really easy to use Spring JPA but i wonder why its so complicated with <strong>multiple datasources.</strong>
  I am trying to access 3 different databases and i have defined corresponding config files for the same and seperate Repositories as well. </p>
  
  <p>Spring is calling all three EntityManagerFactories but when i access them in Service layer, i have access to same db all the time. </p>
  
  <p>Is there anyway to <strong>verify if Autowired Repository is pointing to correct database</strong>?</p>
  
  <p>My <strong>1st db config</strong></p>
  
  <pre><code>  @Configuration
  @EnableTransactionManagement
  @EnableJpaRepositories(basePackages = "xx.xx.xx.xx", entityManagerFactoryRef = "orangeEntityManagerFactory ", transactionManagerRef = "transactionManagerthree")
  @PropertySource("classpath:application.properties")
  public class TestDbConfig {
  
      @Value("${spring.datasource.driver-class-name}")
      String driverClassName = "";
      @Value("${spring.datasource.url}")
      String url = "";
      @Value("${spring.datasource.username}")
      String userName = "";
      @Value("${spring.datasource.password}")
      String password = "";
      @Autowired
      JpaVendorAdapter jpaVendorAdapter;
  
  
      @Bean(name = "orangeEntityManager")
      public EntityManager entityManager() {
          return entityManagerFactory().createEntityManager();
      }
  
      @Bean(name = "orangeEntityManagerFactory")
      public EntityManagerFactory entityManagerFactory() {
          LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
  
          factoryBean.setDataSource(DataSourceBuilder.create().url(url)
                  .driverClassName(driverClassName).username(userName)
                  .password(password).build());
  
          factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
          factoryBean.setPackagesToScan(R.orange_PACKAGE);
          factoryBean.setPersistenceUnitName("orangePersistenceUnit");
          factoryBean.afterPropertiesSet();
  
          return factoryBean.getObject();
      }
  
      @Bean(name = "orangeTransactionManager")
      PlatformTransactionManager transactionManagerThree() {
          return new JpaTransactionManager(entityManagerFactory());
      }
  
  }
  </code></pre>
  
  <p>****2nd db config ****</p>
  
  <pre><code> @Configuration
  @EnableTransactionManagement
  @EnableJpaRepositories(basePackages = "xx.xx.xx.xx", entityManagerFactoryRef = "cscheloEntityManagerFactory ", transactionManagerRef = "transactionManagertwo")
  @PropertySource("classpath:application.properties")
  public class cscDbConfig {
  
      @Value("${spring.datasourcecschelo.driver-class-name}")
      String driverClassName = "";
      @Value("${spring.datasourcecschelo.url}")
      String url = "";
      @Value("${spring.datasourcecschelo.username}")
      String userName = "";
      @Value("${spring.datasourcecschelo.password}")
      String password = "";
      @Autowired
      JpaVendorAdapter jpaVendorAdapter;
  
      // @Bean(name = "cscheloDataSource")
      // public DataSource dataSource() {
      // return DataSourceBuilder.create().url(url)
      // .driverClassName(driverClassName).username(userName)
      // .password(password).build();
      // }
  
      @Bean(name = "cscheloEntityManager")
      public EntityManager entityManager() {
          return entityManagerFactory().createEntityManager();
      }
  
      @Bean(name = "cscheloEntityManagerFactory")
      public EntityManagerFactory entityManagerFactory() {
          LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
  
          factoryBean.setDataSource(DataSourceBuilder.create().url(url)
                  .driverClassName(driverClassName).username(userName)
                  .password(password).build());
  
          factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
          factoryBean.setPackagesToScan(R.cscDB_PACKAGE);
          factoryBean.setPersistenceUnitName("cscheloPersistenceUnit");
          factoryBean.afterPropertiesSet();
          return factoryBean.getObject();
      }
  
      @Bean(name = "transactionManagertwo")
      PlatformTransactionManager transactionManagerTwo() {
          return new JpaTransactionManager(entityManagerFactory());
      }
  
  }
  </code></pre>
  
  <p><strong>3rd db config</strong></p>
  
  <pre><code>  @Configuration
  @EnableTransactionManagement
  @EnableJpaRepositories(basePackages = "xx.xx.xx.xx", entityManagerFactoryRef = "harvEntityManagerFactory ", transactionManagerRef = "transactionManagerOne")
  @PropertySource("classpath:application.properties")
  public class HarvDbConfig {
  
      @Value("${spring.datasourceHarvhelo.driver-class-name}")
      String driverClassName = "";
      @Value("${spring.datasourceHarvhelo.url}")
      String url = "";
      @Value("${spring.datasourceHarvhelo.username}")
      String userName = "";
      @Value("${spring.datasourceHarvhelo.password}")
      String password = "";
      @Autowired
      JpaVendorAdapter jpaVendorAdapter;
  
      @Bean(name = "HarvEntityManager")
      public EntityManager entityManager() {
          return entityManagerFactory().createEntityManager();
      }
  
      @Bean(name = "HarvEntityManagerFactory")
      public EntityManagerFactory entityManagerFactory() {
          LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
  
          factoryBean.setDataSource(DataSourceBuilder.create().url(url)
                  .driverClassName(driverClassName).username(userName)
                  .password(password).build());
          factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
          factoryBean.setPackagesToScan(R.HarvnDB_PACKAGE);
          factoryBean.setPersistenceUnitName("HarvPersistenceUnit");
          factoryBean.afterPropertiesSet();
          return factoryBean.getObject();
      }
  
      @Bean
      PlatformTransactionManager transactionManagerOne() {
          return new JpaTransactionManager(entityManagerFactory());
      }
  
  }
  </code></pre>
  
  <p><strong>My Repositories</strong> </p>
  
  <pre><code>    @Repository
  public interface OrderRepository extends JpaRepository&lt;Order, Long&gt; {
  
      @Query(value = "select * from orders  where id=?1", nativeQuery = true)
      Order getOrders(Long id);
  
      @Query(value = "REPLACE INTO order  ", nativeQuery = true)
      void replaceIntoOrder(Order order);
  
      Order findById(Long id);
  
  }
  
  @Repository
  public interface cscOrderRepository extends JpaRepository&lt;Order, Long&gt; {
      @Query(value = "select * from v_orders  where old_id=?1 and ordertype_id=?2", nativeQuery = true)
      Order findByIdndType(int id, int type);
  }
  
  @Repository
  public interface HarvOrderRepository extends JpaRepository&lt;Order, Long&gt; {
      @Query(value = "select * from v_orders  where old_id=?1 and ordertype_id=?2", nativeQuery = true)
      Order findByIdndType(int id, int type);
  }
  </code></pre>
  
  <p>and this is how i am trying to access them </p>
  
  <pre><code> @Service
  public class OrderService extends AbstractService&lt;Order&gt; {
  
      @Autowired
      OrderRepository orangeOrderRepository;
      @Autowired
      HarvOrderRepository ngheloOrderRepository;
      @Autowired
      cscOrderRepository cscheloOrderRepository;
  
      @Override
      public Order findById(Long id) throws EntityNotFoundException {
          int idToBePassed = 123223;
          int type = 1;
  
          Order ngheloOrder = ngheloOrderRepository.findByIdndType(idToBePassed,
                  type);
          Order cscheloOrder = cscheloOrderRepository.findByIdndType(
                  idToBePassed, type);
  </code></pre>
  
  <p>As can be seen in code i am autowiring all 3 repositories from 3 db's ,i want to fetch data from one db and insert in another(as per my requirement) but when i tried accessing <strong>cscheloOrderRepository.findByIdndType(
                  idToBePassed, type);</strong> it says table xyz do not exist ,because it is not accessing the right EntityManagerFactory settings. </p>
  
  <p>Moreover i do not know how to Autowire EntityManagerFactory manually in Repositories,i tried with PersistentContext but got exception that bean not found.</p>
  
  <p>Please provide your valuable feedback. how can i resolve this problem , Thanks.</p>