title: How do you use Spring Data JPA outside of a Spring Container?
body: <p>I'm trying to wire up Spring Data JPA objects manually so that I can generate DAO proxies (aka Repositories) - without using a Spring bean container.</p>
  
  <p>Inevitably, I will be asked why I want to do this: it is because our project is already using Google Guice (and on the UI using Gin with GWT), and we don't want to maintain another IoC container configuration, or pull in all the resulting dependencies. I know we might be able to use Guice's <code>SpringIntegration</code>, but this would be a last resort.</p>
  
  <p>It seems that everything is available to wire the objects up manually, but since it's not well documented, I'm having a difficult time.</p>
  
  <p>According to the Spring Data user's guide, using <a href="http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/#repositories.create-instances.standalone" rel="noreferrer">repository factories standalone</a> is possible. Unfortunately, the example shows <code>RepositoryFactorySupport</code> which is an abstract class. After some searching I managed to find <a href="http://static.springsource.org/spring-data/data-jpa/docs/current/api/index.html?org/springframework/data/jpa/repository/support/JpaRepositoryFactory.html" rel="noreferrer"><code>JpaRepositoryFactory</code></a></p>
  
  <p><code>JpaRepositoryFactory</code> actually works fairly well, except it does not automatically create transactions. Transactions must be managed manually, or nothing will get persisted to the database:</p>
  
  <pre><code>entityManager.getTransaction().begin();
  repositoryInstance.save(someJpaObject);
  entityManager.getTransaction().commit();
  </code></pre>
  
  <p>The problem turned out to be that <code>@Transactional</code> annotations are not used automatically, and need the help of a <code>TransactionInterceptor</code></p>
  
  <p>Thankfully, the <code>JpaRepositoryFactory</code> can take a callback to add more AOP advice to the generated Repository proxy before returning:</p>
  
  <pre><code>final JpaTransactionManager xactManager = new JpaTransactionManager(emf);
  final JpaRepositoryFactory factory = new JpaRepositoryFactory(emf.createEntityManager());
  
  factory.addRepositoryProxyPostProcessor(new RepositoryProxyPostProcessor() {
      @Override
      public void postProcess(ProxyFactory factory) {
          factory.addAdvice(new TransactionInterceptor(xactManager, new AnnotationTransactionAttributeSource()));
      }
  });
  </code></pre>
  
  <p>This is where things are not working out so well. Stepping through the debugger in the code, the <code>TransactionInterceptor</code> is indeed creating a transaction - but on the wrong <code>EntityManager</code>. Spring manages the active <code>EntityManager</code> by looking at the currently executing thread. The <code>TransactionInterceptor</code> does this and sees there is no active <code>EntityManager</code> bound to the thread, and decides to create a new one.</p>
  
  <p>However, this new <code>EntityManager</code> is not the same instance that was created and passed into the <code>JpaRepositoryFactory</code> constructor, which requires an <code>EntityManager</code>. The question is, how do I make the <code>TransactionInterceptor</code> and the <code>JpaRepositoryFactory</code> use the same <code>EntityManager</code>?</p>
  
  <h2>Update:</h2>
  
  <p>While writing this up, I found out how to solve the problem but it still may not be the ideal solution. I will post this solution as a separate answer. I would be happy to hear any suggestions on a better way to use Spring Data JPA standalone than how I've solve it.</p>