2

Is there a way to integrate spring batch with spring data? I see RepositoryItemReader and RepositoryItemWriter in spring documentation.

Yun
  • 3,056
  • 6
  • 9
  • 28
kaissun
  • 3,044
  • 4
  • 20
  • 35
  • 2
    That's the way to do it. Was there something else you were looking for? – Michael Minella Mar 24 '17 at 15:42
  • well i'm doing that in a different way i'm injecting the service into the job configuration and invoke the method that's available on the JpaRepository, like this example @ Bean @ StepScope public ItemWriter customerItemWriter2() { return items -> { for (Customer item : items) { customerService.save(item); } }; } – kaissun Mar 24 '17 at 16:17
  • Why would you do that when we have the `RepositoryItemWriter` for you? – Michael Minella Mar 24 '17 at 20:35
  • I have discovered it this day by hazard it will be better to use it,and I would like to thank you for all the course that you have made on O'Reilly it really helped me a lot. – kaissun Mar 24 '17 at 20:47
  • @MichaelMinella could you answer my other question please thanks http://stackoverflow.com/questions/43008684/local-partionnig-in-spring-batch – kaissun Mar 25 '17 at 19:27

2 Answers2

3

You are totally right. Spring batch can be easily be integrated with spring data. Here example of item reader:

    @Bean(name = "lotteryInfoReader")
    @StepScope
    public RepositoryItemReader<LotteryInfo> reader() {
        RepositoryItemReader<LotteryInfo> reader = new RepositoryItemReader<>();
        reader.setRepository(lotteryInfoRepository);
        reader.setMethodName("findAll");
        reader.setSort(Collections.singletonMap("name", Sort.Direction.ASC));
        return reader;
    }

Here is another example with using hibernate without spring data :

    @Bean(name = "drawsWriter")
    @StepScope
    public ItemWriter<? super List<Draw>> writer() {
        return items -> items.stream()
                .flatMap(Collection::stream)
                .forEach(entityManager::merge);
    }
smaiakov
  • 470
  • 5
  • 20
0

well i'm doing that in a different way i'm injecting the service into the job configuration and invoke the method that's available on the JpaRepository, like this example

    @Bean
    @StepScope
    public ItemWriter<Customer> customerItemWriter2() {
        return items -> {
            for (Customer item : items) {

                customerService.save(item);
            }
        };
    }
kaissun
  • 3,044
  • 4
  • 20
  • 35