title: How to inject multiple JPA EntityManager (persistence units) when using Spring
body: <p>I need to use one database for queries (non-modifying) and one for commands (modifying). I am using Spring Data JPA, so I have two configuration classes:</p>
  
  <pre><code>@Configuration
  @EnableJpaRepositories(value = "com.company.read",
          entityManagerFactoryRef = "readingEntityManagerFactory",
          transactionManagerRef = "readingTransactionManager")
  @EnableTransactionManagement
  public class SpringDataJpaReadingConfiguration {
  
      @Bean(name = "readingEntityManagerFactory")
      public EntityManagerFactory readingEntityManagerFactory() {
          return Persistence.createEntityManagerFactory("persistence.reading");
      }
  
      @Bean(name = "readingExceptionTranslator")
      public HibernateExceptionTranslator readingHibernateExceptionTranslator() {
          return new HibernateExceptionTranslator();
      }
  
      @Bean(name = "readingTransactionManager")
      public JpaTransactionManager readingTransactionManager() {
          return new JpaTransactionManager();
      }
  
  }
  
  @Configuration
  @EnableJpaRepositories(value = "com.company.write",
          entityManagerFactoryRef = "writingEntityManagerFactory",
          transactionManagerRef = "writingTransactionManager")
  @EnableTransactionManagement
  public class SpringDataJpaWritingConfiguration {
  
      @Bean(name = "writingEntityManagerFactory")
      public EntityManagerFactory writingEntityManagerFactory() {
          return Persistence.createEntityManagerFactory("persistence.writing");
      }
  
      @Bean(name = "writingExceptionTranslator")
      public HibernateExceptionTranslator writingHibernateExceptionTranslator() {
          return new HibernateExceptionTranslator();
      }
  
      @Bean(name = "writingTransactionManager")
      public JpaTransactionManager writingTransactionManager() {
          return new JpaTransactionManager();
      }
  
  }
  </code></pre>
  
  <p>In my repository I sometimes need to decide with EntityManager to use like so:</p>
  
  <pre><code>@Repository
  public class UserReadingRepository {
  
      @PersistenceContext(unitName = "persistence.reading")
      private EntityManager em;
  
      // some useful queries here
  }
  </code></pre>
  
  <p>I am using persistence unit's name as defined in my <strong>persistence.xml</strong>:</p>
  
  <pre><code>&lt;persistence xmlns="http://java.sun.com/xml/ns/persistence"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
               version="2.0"&gt;
  
      &lt;persistence-unit name="persistence.reading" transaction-type="RESOURCE_LOCAL"&gt;
          &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
          &lt;non-jta-data-source&gt;ReadingDS&lt;/non-jta-data-source&gt;
          &lt;properties&gt;
              &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /&gt;
              &lt;property name="hibernate.show_sql" value="true" /&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  
      &lt;persistence-unit name="persistence.writing" transaction-type="RESOURCE_LOCAL"&gt;
          &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
          &lt;non-jta-data-source&gt;WritingDS&lt;/non-jta-data-source&gt;
          &lt;properties&gt;
              &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /&gt;
              &lt;property name="hibernate.show_sql" value="true" /&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  
  &lt;/persistence&gt;
  </code></pre>
  
  <p>Spring throws <code>org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'persistence.reading'</code> is defined. Oddly, it looks like Spring tries to instantiate <em>a bean</em> with persistence unit name? Did I misconfigure something?</p>
  
  <p><strong>UPDATE</strong>: When I remove <code>unitName = "persistence.reading"</code> from @PersistenceContext annotation, I will get following error instead:
  <code>org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2: readingEntityManagerFactory,writingEntityManagerFactory</code></p>
  
  <p><strong>UPDATE 2</strong>: Rohit suggested (in the comment) to wire <code>EntityManagerFactory</code> instead. So I tried to do the following:</p>
  
  <pre><code>@PersistenceUnit(unitName = "persistence.reading")
  private EntityManagerFactory emf;
  </code></pre>
  
  <p>but Spring only reports:  <code>org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'persistence.reading' is defined</code></p>
  
  <p><strong>FINAL FIX</strong>:
  Thanks to Vlad's answer, I was able to update the code to use the following (just make sure you define your <code>dataSource</code> bean as well):</p>
  
  <pre><code>@Bean(name = "readingEntityManagerFactory")
  public EntityManagerFactory readingEntityManagerFactory() {
      LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
      em.setPersistenceUnitName("persistence.reading");
      em.setDataSource(dataSource());
      em.setPackagesToScan("com.company");
      em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
      em.afterPropertiesSet();
      return em.getObject();
  }
  </code></pre>