title: Spring + Hibernate + Quartz: Dynamic Job
body: <p>I want to create dynamically jobs using Quartz, Spring and Hibernate. Users interact with a web service to create <strong>jobs</strong> of this class:</p>
  
  <pre><code>public class StartJobSpring extends QuartzJobBean {
  
      private String jobId;
      private String jobType;
  
      @Autowired
      private NoaJobInstancesDAO njiDAO;
  
      @Transactional
      @Override
      protected void executeInternal(JobExecutionContext context)
              throws JobExecutionException {
  
          JobKey key = context.getJobDetail().getKey();
          JobDataMap dataMap = context.getMergedJobDataMap();
  
          // some logic
          njiDAO.create(instanceUUID, noaJob.getNoaJob(jobId), jobType);
      }
  }
  </code></pre>
  
  <p>The <strong>NoaJobInstancesDAO</strong> is a simple DAO class which makes use of Hibernate's <strong>EntityManager</strong>:</p>
  
  <pre><code>@Repository
  public class NoaJobInstancesDAOHibImpl implements NoaJobInstancesDAO {
  
      @PersistenceContext
      private EntityManager entityManager;
  
      @Override
      @Transactional
      public NoaJobInstanceJPA create(NoaJobInstanceJPA entity) {
          entityManager.persist(entity);
          return entity;
      }
  
      @Override
      public void create(String instance_uuid, NoaJobJPA job, String job_type) {
          NoaJobInstanceJPA entity = new NoaJobInstanceJPA(instance_uuid, job,
                  job_type, "CREATED", null, null, "", "N", "N");
          this.create(entity);
      } 
  }
  </code></pre>
  
  <p>The problem is that when this job fires, an exception is thrown:</p>
  
  <pre><code>javax.persistence.TransactionRequiredException: No transactional EntityManager available
  </code></pre>
  
  <p>and i can't understand why! 
  I schedule the job in this way in a <strong>Manager</strong> class</p>
  
  <pre><code>JobDetail job = newJob(StartJobSpring.class).withIdentity(//anId)
                  .setJobData(//aJobMap).build();
  getScheduler().getObject().scheduleJob(job, trigger);
  </code></pre>
  
  <p>where the scheduler is wired to the manager as</p>
  
  <pre><code>@Autowired
  private ApplicationContext applicationContext;
  
  @Bean
  SchedulerFactoryBean schedulerFactoryBean(DataSource dataSource, JpaTransactionManager transactionManager) {
  
      SchedulerFactoryBean bean = new SchedulerFactoryBean();
  
      AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
      jobFactory.setApplicationContext(applicationContext);
      bean.setJobFactory(jobFactory);
  
      bean.setTransactionManager(transactionManager);
  
      return bean;
  }
  </code></pre>
  
  <p>The class AutowiringSpringBeanJobFactory is the same as <a href="https://gist.github.com/jelies/5085593" rel="nofollow">Autowiring</a>.</p>
  
  <p>In my opion there's something wrong in the scheduler wiring. In fact, I don't understand how I can retrieve the application context.</p>
  
  <p><strong>EDIT1:</strong> The application context seems to be correctly instanced. The problem could not be there.</p>
  
  <p><strong>EDIT2:</strong> I'm using a single configuration bean (not xml files). Here the main methods:</p>
  
  <pre><code>@Bean
  LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource   dataSource) {
       LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
       entityManagerFactoryBean.setDataSource(dataSource);
       entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
       entityManagerFactoryBean.setPackagesToScan("package");
  
      Properties jpaProperties = new Properties();
      jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.OracleDialect");
      jpaProperties.put("hibernate.show_sql", "false");
      jpaProperties.put("hibernate.hbm2ddl.auto", "update");
  
      entityManagerFactoryBean.setJpaProperties(jpaProperties);
  
      return entityManagerFactoryBean;
  }
  
  @Bean
  JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
      JpaTransactionManager transactionManager = new JpaTransactionManager();
      transactionManager.setEntityManagerFactory(entityManagerFactory);
      return transactionManager;
  }
  
  @Bean
  public NoaJobInstancesDAO noaJobInstancesDAO() {
      NoaJobInstancesDAOHibImpl noaJobInstancesDAO = new NoaJobInstancesDAOHibImpl();
      return noaJobInstancesDAO;
  }
  </code></pre>