title: How to use multiple JPA persistence contexts with Spring Data JPA?
body: <p>I need two persistence contexts to manage entities in separate databases. </p>
  
  <p>I am using java based config with spring 4.1.4, hibernate 4.3.7 and spring data 1.2. </p>
  
  <p>Basically, CompanyApplicationContext imports two persistence contexts: PersistenceContext and SharedDbPersistenceContext. Each persistence context with its own data source, transaction manager and entity manager factory. The entities and jpa repositories for each persistence context are present in separate packages and are mentioned in persistence context class files.</p>
  
  <p>I get this error:</p>
  
  <pre><code>Not an managed type: class com.company.shared.model.ClientSetting
  </code></pre>
  
  <p>Full exception:</p>
  
  <pre><code>Caused by: java.lang.IllegalArgumentException: Not an managed type: class com.company.shared.model.ClientSetting
      at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:219)
  </code></pre>
  
  <p>What extra configuration do I need to do this to make it work? Why is each context not scanning and registering entities and repositories properly?</p>
  
  <p>Main application context:</p>
  
  <pre><code>package com.company.config;
  @Configuration
  @ComponentScan(basePackages
          = "com.company"
  )
  @Import({
      PersistenceContext.class,
      SharedDbPersistenceContext.class})
  public class CompanyApplicationContext extends WebMvcConfigurerAdapter {
  
  }
  </code></pre>
  
  <p>Persistence Context:</p>
  
  <pre><code> package com.company.config;
      @Configuration
      @EnableTransactionManagement
      @EnableJpaRepositories(
              basePackages = {"com.company.todo.repository"})
      public class PersistenceContext {
  
          @Resource
          private Environment environment;
  
          @Bean
          public DataSource dataSource() {
              BoneCPDataSource dataSource = new BoneCPDataSource();
              //init data source
              return dataSource;
          }
  
  
          @Bean
          public JpaTransactionManager transactionManager() {
              JpaTransactionManager transactionManager = new JpaTransactionManager();
              transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
              return transactionManager;
          }
  
          @Bean
          public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
              LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
  
              entityManagerFactoryBean.setDataSource(dataSource());
              entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
              entityManagerFactoryBean.setPackagesToScan("com.company.todo.model");
              Properties jpaProperties = new Properties();
              //init jpa properties
              entityManagerFactoryBean.setJpaProperties(jpaProperties);
              return entityManagerFactoryBean;
          }
  
      }
  </code></pre>
  
  <p>Shared Db Persistence Context:</p>
  
  <pre><code>    package com.company.config;
      @Configuration
      @EnableTransactionManagement
      @EnableJpaRepositories(
              basePackages = {"com.company.shared.repository"})
      public class SharedDbPersistenceContext {
  
          @Resource
          private Environment environment;
  
          @Bean
          public DataSource sharedDataSource() {
              //init and return data source
          }
  
          @Bean
          public JpaTransactionManager sharedDbTransactionManager() {
  //init transaction manager
          }
  
          @Bean
          public LocalContainerEntityManagerFactoryBean sharedEntityManagerFactory() {
              LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
  
              entityManagerFactoryBean.setDataSource(sharedDataSource());
              entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
              entityManagerFactoryBean.setPackagesToScan("com.company.shared.model");
              Properties jpaProperties = new Properties();
              //init jpaProperties
              entityManagerFactoryBean.setJpaProperties(jpaProperties);
              return entityManagerFactoryBean;
          }
      }
  </code></pre>
  
  <p>Repository:</p>
  
  <pre><code>package com.company.shared.repository;
  @Repository
  public interface ClientSettingsRepository extends JpaRepository&lt;ClientSetting, Integer&gt; {}
  </code></pre>
  
  <p>Entity:</p>
  
  <pre><code>package com.company.shared.model;
  @Entity
  public class ClientSetting implements Serializable {
  //props
  }
  </code></pre>
  
  <p>Service:</p>
  
  <pre><code>package com.company.shared.service;
  @Service
  public class DataSourceServiceImpl implements DataSourceService{
  
      @Autowired
      ClientSettingsRepository clientSettingsRepository;
  
      @Override
      public List&lt;ClientSetting&gt; findClientDataSourceSettings() {
  
          return clientSettingsRepository.findClientDataSourceSettings();
      }
  
  }
  </code></pre>