title: JpaItemWriter: no transaction is in progress
body: <p>I'd like to use JpaItemWriter to batch persist entities. But when I use the following code to persist, I'm told:</p>
  
  <pre><code>Hibernate: 
      select
          nextval ('hibernate_sequence')
  [] 2014-03-19 15:46:02,237 ERROR : TransactionRequiredException: no transaction is in progress
  </code></pre>
  
  <p>How can I enable transactions on the following:</p>
  
  <pre><code>@Bean
  public ItemWriter&lt;T&gt; writer() {
      JpaItemWriter&lt;T&gt; itemWriter = new JpaItemWriter&lt;&gt;();
      itemWriter.setEntityManagerFactory(emf);
  }
  
  @Configuration
  @EnableTransactionManagement
  @EnableBatchProcessing 
  class Config{ {
       @Bean
      public LocalContainerEntityManagerFactoryBean emf() {
          LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
          emf.setDataSource(dataSource());
          emf.setPackagesToScan("my.package");
          emf.setJpaVendorAdapter(jpaAdapter());
          emf.setJpaProperties(jpaProterties());
          return emf;
  }
  </code></pre>
  
  <p>Edit:</p>
  
  <pre><code>@Bean
  public Job airlineJob(JobBuilderFactory jobs, Step step) {
      return jobs.get("job")
              .start(step)
              .build();
  }
  
  //Reader is a `FlatFileItemReader`, writer is `CustomItemWriter`.
  @Bean
  public Step step(StepBuilderFactory steps,
          MultiResourceItemReader&lt;T&gt; rea,
          ItemProcessor&lt;T, T&gt; pro,
          ItemWriter&lt;T&gt; wr) {
      return steps.get("step")
              .reader(rea)
              .processor(proc)
              .writer(wr)
              .build();
  }
  
  //use same datasource and tx manager as in the full web application
  @Bean
  public JobLauncher launcher(TransactionManager tx, DataSource ds) throws Exception {
      SimpleJobLauncher launcher = new SimpleJobLauncher();
  
      JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
      factory.setDataSource(ds);
      factory.setTransactionManager(tx);
  
      jobLauncher.setJobRepository(factory.getJobRepository());
      return launcher;
  }
  </code></pre>
  
  <hr>
  
  <p>Edit 2 as response to @Haim:</p>
  
  <pre><code>@Bean
      public JpaItemWriter&lt;T&gt; jpaItemWriter(EntityManagerFactory emf) {
          JpaItemWriter&lt;T&gt; writer = new JpaItemWriter&lt;T&gt;();
          writer.setEntityManagerFactory(emf);
          return writer;
      }
  </code></pre>