kaissun
kaissun

Reputation: 3086

Spring batch with spring data

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

Upvotes: 2

Views: 7533

Answers (2)

smaiakov
smaiakov

Reputation: 480

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);
    }

Upvotes: 3

kaissun
kaissun

Reputation: 3086

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);
            }
        };
    }

Upvotes: 0

Related Questions