title: Spring equivalent of @Stateless EJB with multiple @PersistenceContext
body: <p>I've been banging my head on the wall for the past couple of days trying to get this to work but I've not been able to.</p>
  
  <p>I wrote a JPA EntityManagerProvider recently using EJB:</p>
  
  <p><strong>EntityManagerProviderBean.java</strong></p>
  
  <pre><code>@Stateless
  public class EntityManagerProviderBean {
  
      @PersistenceContext(unitName="PU1")
      private EntityManager entityManager1;   
  
      @PersistenceContext(unitName="PU2")
      private EntityManager entityManager2;
  
      public EntityManager getEntityManager() {
          return (...) ? entityManager1: entityManager2;
      }
  }
  </code></pre>
  
  <p>And then of course I can inject the EJB wherever needed like this:</p>
  
  <p><strong>UserFacade.java</strong></p>
  
  <pre><code>@Stateless
  public class UserFacade {
  
      @EJB
      private EntityManagerProviderBean emProvider;
      private EntityManager em = emProvider.getEntityManager();
  
      ...
  }
  </code></pre>
  
  <p>Now I'm trying to do something similar using Spring, using annotations, and without doing anything in XML.  I can't seem to figure out a way to inject the EntityManager. Anything that I do leads to a NullPointerException. For example, I tried to inject the EntityManager manually without relying on my EntityManagerProviderBean, like this:</p>
  
  <p><strong>UserFacadeSpring.java</strong></p>
  
  <pre><code>public class UserFacadeSpring {
  
      @PersistenceContext(unitName="PU1")
      private EntityManager em;
  
      ...
  }
  </code></pre>
  
  <p>But this gives me a NullPointerException. So the EntityManager is not being injected at all and I'm not sure what's wrong.</p>
  
  <p>So two questions basically:</p>
  
  <ol>
  <li>How can I inject the EntityManager using Spring?</li>
  <li>How can I use my existing EntityManagerProviderBean EJB in Spring? What modifications do I need to make?</li>
  </ol>
  
  <p>Any help in this matter will be greatly appreciated. As you can tell I'm a complete noob to Spring. I tried to read the guide but everything's flying over my head at the moment. I actually did try to do something half-baked but it didn't work either (I either get NullPointerException or BeanNotFoundException, I must have used every combination of @Component, @Bean, @Autowired annotations I think!):</p>
  
  <p><strong>EntityManagerProviderSpring.java</strong></p>
  
  <pre><code>@Component
  public class EntityManagerProviderSpring {
  
      @PersistenceContext(unitName="PU1")
      private EntityManager entityManager1;   
  
      @PersistenceContext(unitName="PU2")
      private EntityManager entityManager2;
  
      @Bean
      public EntityManager getEntityManager() {
          return (...) ? entityManager1: entityManager2;
      }
  }
  </code></pre>
  
  <p><strong>Main.java</strong></p>
  
  <pre><code>public class Main {
      public static void main(String[] args) {
  
      ApplicationContext context = new AnnotationConfigApplicationContext(EntityManagerProviderSpring.class);
      EntityManagerProviderSpring emProvider = context.getBean(EntityManagerProviderSpring.class);
      EntityManager em = emProvider.getEntityManager();
  
      ...
  }
  </code></pre>
  
  <p>Thanks!</p>
  
  <p>EDIT: I'm using Hibernate with JPA and the following is my persistence.xml file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"&gt;
    &lt;persistence-unit name="PU1" transaction-type="RESOURCE_LOCAL"&gt;
      &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
      &lt;class&gt;...&lt;/class&gt;
      &lt;properties&gt;
        &lt;property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/db1?zeroDateTimeBehavior=convertToNull"/&gt;
        &lt;property name="javax.persistence.jdbc.user" value="pu1_username"/&gt;
        &lt;property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/&gt;
        &lt;property name="javax.persistence.jdbc.password" value="pu1_pwd"/&gt;
        &lt;property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/&gt;
      &lt;/properties&gt;
    &lt;/persistence-unit&gt;
    &lt;persistence-unit name="PU2" transaction-type="RESOURCE_LOCAL"&gt;
      ...
    &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>