title: Using PersistenceContext in a Quartz Job
body: <p>We're using Spring 3.1, JPA (via Hibernate) and Quartz.  Typically we interact with the DB via @PersistenceContext annotation on Service beans, and either SpringMVC controllers, or GraniteDS-managed service invocation.</p>
  
  <p>I'm working on writing a Quartz job that needs to interact with the database.  I've tried everything I can find to get this working.  I tried passing in a Spring-managed component (annotated with @PersistenceContext and @Transactional) via the jobMap, the call to entityManager.persist(o) executes, but nothing happens in the database.  I also tried similar to <a href="https://stackoverflow.com/a/15211030/2280576">this answer</a>, creating a factory class to call autowireBean() on the job object.  I set up the job class like so:</p>
  
  <pre><code>public class CreateAlertJob implements Job {
  
      @PersistenceContext
      EntityManager entityManager;
  
      @Override
      @Transactional
      public void execute(JobExecutionContext context) throws JobExecutionException {
  
          SomeEntity entity = new SomeEntity();
          entityManager.persist(entity);
      }
  }
  </code></pre>
  
  <p>Same result, the method executes but the database is unaltered.  I found <a href="http://blog.tirasa.net/blogs/index.php/ilgrosso/aop-n-spring-n-jpa" rel="nofollow noreferrer">this blog post</a> which references a GitHub project.  There he is using JpaInterceptor to establish a Hibernate session, but this uses the DAO pattern and I'd like to stick with using @PersistenceContext.</p>
  
  <p>Clearly there is something about the Quartz thread that is preventing this from working properly?  I'm about out of ideas and considering making a web service call to a SpringMVC controller just to get this working.</p>